We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
const asyncMethod = () => new Promise((resolve, reject) => { console.log('1') // 执行异步方法 setTimeout(() => { resolve('6-success') reject('6-error') console.log('5') }, 10) console.log('2') }) const executeByPromise = () => { asyncMethod() .then((data) => console.log(data)) .catch(error => console.log(error)) console.log('3') console.log('4') } executeByPromise() // 1 2 3 4 5 6-success
const asyncMethod = () => new Promise((resolve, reject) => { console.log('1') // 执行异步方法 setTimeout(() => { resolve('6-success') reject('6-error') console.log('5') }, 10) console.log('2') }) const executeByPromise = () => { asyncMethod() console.log('3') console.log('4') } executeByPromise() // 1 2 3 4 5
const asyncMethod = () => new Promise((resolve, reject) => { console.log('1') // 执行异步方法 setTimeout(() => { reject('6-error') resolve('6-success') console.log('5') }, 10) console.log('2') }) const executeByPromise = () => { asyncMethod() .then((data) => console.log(data)) .catch(error => console.log(error)) console.log('3') console.log('4') } executeByPromise() // 1 2 3 4 5 6-error
const asyncMethod = () => new Promise((resolve, reject) => { console.log('1') // 执行异步方法 setTimeout(() => { reject('6-error') resolve('6-success') console.log('5') }, 10) console.log('2') }) const executeByPromise = () => { asyncMethod() console.log('3') console.log('4') } executeByPromise() // 1 2 3 4 5 Uncaught (in promise) 6-error
const asyncMethod = () => new Promise((resolve, reject) => { console.log('1') // 执行异步方法 setTimeout(() => { console.log('6') }, 10) // 错误写法:将回调写在了异步方法之外 reject('5-error') console.log('2') }) const executeByPromise = () => { asyncMethod() console.log('3') console.log('4') } executeByPromise() // 1 2 3 4 Uncaught (in promise) 5-error 6
const asyncMethod = () => new Promise((resolve, reject) => { console.log('1') // 执行异步方法 setTimeout(() => { console.log('6') }, 10) // 错误写法:将回调写在了异步方法之外 resolve('5-success') console.log('2') }) const executeByPromise = () => { asyncMethod() console.log('3') console.log('4') } executeByPromise() // 1 2 3 4 6
Promise关联方法:resolve(value)和reject(error)。
resolve(value)
reject(error)
Promise关联方法位于异步回调内:
resolve
reject
resolve(error)
error
then
value
Promise关联方法位于异步回调外:
const asyncMethod = () => new Promise(resolve => { console.log('1') // 执行异步方法 setTimeout(() => { resolve('6-success') console.log('3') }, 10) console.log('2') }) const executeByKeyword = async () => { const result = await asyncMethod() console.log('4') console.log('5') console.log(result) } executeByKeyword() // 1 2 3 4 5 6-success
const asyncMethod = () => new Promise(resolve => { console.log('1') // 执行异步方法 setTimeout(() => { resolve('4-success') console.log('3') }, 10) console.log('2') }) const executeByKeyword = async () => { const result = await asyncMethod() console.log(result) console.log('5') console.log('6') } executeByKeyword() // 1 2 3 4-success 5 6
const asyncMethod = () => new Promise((resolve, reject) => { console.log('1') // 执行异步方法 setTimeout(() => { reject('4-error') console.log('3') }, 10) console.log('2') }) const executeByKeyword = async () => { const result = await asyncMethod() console.log('5') console.log('6') console.log(result) } executeByKeyword() // 1 2 3 Uncaught (in promise) 4-error
const asyncMethod = () => new Promise(resolve => { console.log('1') // 执行异步方法 setTimeout(() => { console.log('6') }, 10) // 错误写法:将回调写在了异步方法之外 resolve('3-success') console.log('2') }) const executeByKeyword = async () => { const result = await asyncMethod() console.log(result) console.log('4') console.log('5') } executeByKeyword() // 1 2 3-success 4 5 6
const asyncMethod = () => new Promise(resolve => { console.log('1') // 执行异步方法 setTimeout(() => { console.log('6') }, 10) // 错误写法:将回调写在了异步方法之外 resolve('5-success') console.log('2') }) const executeByKeyword = async () => { const result = await asyncMethod() console.log('3') console.log('4') console.log(result) } executeByKeyword() // 1 2 3 4 5-success 6
const asyncMethod = () => new Promise((resolve, reject) => { console.log('1') // 执行异步方法 setTimeout(() => { console.log('6') }, 10) // 错误写法:将回调写在了异步方法之外 reject('3-error') console.log('2') }) const executeByKeyword = async () => { const result = await asyncMethod() console.log('4') console.log('5') console.log(result) } executeByKeyword() // 1 2 Uncaught (in promise) 3-error 6
await
因为async/await只作为用来简化使用基本Promise API的语法,所以Promise的特性,在异步函数中同样有效。
async
promise
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Promise
常规
非常规
总结
Promise关联方法位于异步回调内:
resolve
与reject
,哪个在前就执行哪个)- 若方法为
resolve(error)
,则自动抛出错误error
- 若方法为
resolve(value)
,则只有通过then
才能得到value
Promise关联方法位于异步回调外:
resolve(error)
,则自动报出错误error
resolve(value)
,则只通过then
才能得到value
异步函数(async & await)
常规
非常规
Promise关联方法位于异步回调内:
await
关键字时,按顺序执行完其表达式Promise中的同步方法reject(error)
时,则直接抛出错误error
await
表达式的下一条语句await
表达式的结果,执行resolve(value)
方法得到结果值value
await
表达式的结果,执行该条语句,直到遇到使用该await
表达式的结果值时,才调用resolve
方法Promise关联方法位于异步回调外:
await
关键字时,按顺序执行完其表达式Promise中的非Promise关联方法的同步方法reject(error)
error
await
表达式的下一条语句await
表达式的结果,执行resolve(value)
方法得到结果值value
await
表达式的结果,执行该条语句,直到遇到使用该await
表达式的结果值时,才调用resolve
方法Promise与Async函数的共同点
reject
也会自动抛出错误resolve
在没有显示调用时,其回调值会被忽略promise
对象本身也是一个异步方法The text was updated successfully, but these errors were encountered: