custom promise resolve All In One
custom promise resolve All In One
ok / err
custom promise resolve
/ reject
const promise = new Promise((ok, err) => {
log(`resolve vs ok`);
// resolve callback, ok
ok(`✅`);
// reject callback, err
err(`❌`)
});
promise.then(v => console.log(v))
// resolve vs ok
// Promise {<fulfilled>: '✅'}
// ✅
demos
console.log(1);
setTimeout(() => {
console.log(2);
Promise.resolve().then(() => console.log(3))
}, 100);
Promise.resolve().then(() => console.log(4))
// new Promise 里面的 console.log 立即执行 ✅
new Promise((resolve, reject) => {
console.log(`✅`);
resolve(`✅`)
});
new Promise((resolve, reject) => {
console.log(`❌`);
reject(`❌`)
}).catch(err => {
console.log(`error =`, err);
});
console.log(5);
setTimeout(() => {
console.log(6);
Promise.resolve().then(() => console.log(7))
console.log(8);
}, 0);
console.log(9)
// 1
// 5
// 9
// 4
// 6
// 8
// 7
// 2
// 3
/*
$ node ./app.js
1
✅
❌
5
9
4
error = ❌
6
8
7
2
3
*/
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://cdn.xgqfrms.xyz/promise/understanding-javascript-promises.pdf
https://www.cnblogs.com/xgqfrms/p/13983194.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17973733
未经授权禁止转载,违者必究!