标签:erro error return end err 选择 异步 定义 ESS
new Promise((resolve, reject) => {
setTimeout(() => {
// 成功时调用resolve
resolve(‘你好‘)
//失败时调用reject
reject(‘error message‘)
}, 1000)
}).then((data) => {
console.log(data) //你好
}).catch((err) => {
console.log(err)
})
new Promise((resolve, reject) => {
setTimeout(() => {
resolve(‘你好‘)
reject(‘error message‘)
}, 1000)
}).then((data) => {
console.log(data)
}, (err) => {
console.log(err)
})
new Promise((resolve) => {resolve(res + ‘111‘)})
new Promise((resolve) => {resolve(res + ‘111‘)})
可简写成Promise.resolve(res + ‘111‘)
Promise.resolve(res + ‘111‘)
还可以进一步简写为res + ‘111‘
new Promise((resolve, reject) => {
setTimeout(() => {
resolve(‘aaa‘)
}, 1000)
}).then(res => {
console.log(res, ‘第一层的10行代码‘)
// return new Promise((resolve) => {resolve(res + ‘111‘)})
return res + ‘111‘
}).then(res => {
console.log(res, ‘第二层的10行代码‘)
return Promise.reject(‘err message‘)
}).then(res => {
console.log(res, ‘第三层的10行代码‘)
}).catch(err => {
console.log(err)
})
Promise.all([new Promise(resolve => {
setTimeout(() => {
resolve(‘result1‘)
}, 2000)
}),
new Promise(resolve => {
setTimeout(() => {
resolve(‘result2‘)
}, 1000)
})
]).then(results => {
console.log(results[0], results[1])
})
标签:erro error return end err 选择 异步 定义 ESS
原文地址:https://www.cnblogs.com/jincanyu/p/14352650.html