突然听说Promise现在很流行,虽然之前有看到过这个,但是没怎么去了解,今天听到就刚好学习记录一下
然后就看了一下廖雪峰老是关于Promise的描述,讲得很详细,提出里面的例子来参考学习用
new Promise(function (resolve, reject) { log('start new Promise...'); var timeOut = Math.random() * 2; log('set timeout to: ' + timeOut + ' seconds.'); setTimeout(function () { if (timeOut < 1) { log('call resolve()...'); resolve('200 OK'); } else { log('call reject()...'); reject('timeout in ' + timeOut + ' seconds.'); } }, timeOut * 1000); }).then(function (r) { log('Done: ' + r); }).catch(function (reason) { log('Failed: ' + reason); });
还有一些内容,详细可以后续继续参考老师的内容学习
https://www.liaoxuefeng.com/wiki/1022910821149312/1023024413276544