Promise thenable All In One
2024-09-04 00:19:34
Promise thenable All In One
Promise thenable 是指一个函数或一个对象的里面定义了一个 then 方法
Promises/A+
https://promisesaplus.com/#terminology
https://github.com/then/promise
https://github.com/then/thenable/blob/master/index.js
// Wrapped
function Wrapped(thenable) {
this.val = thenable;
}
Wrapped.prototype.unwrap = function () {
return this.val;
};
// wrap
function wrap(thenable) {
return new Wrapped(thenable);
}
// unwrap
function unwrap(wrapped) {
return wrapped instanceof Wrapped ? wrapped.unwrap() : wrapped;
}
exports.wrap = wrap;
exports.unwrap = unwrap;
ECMA 262
https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
refs
https://javascript.info/promise-chaining
class Thenable {
constructor(num) {
this.num = num;
}
then(resolve, reject) {
alert(resolve); // function() { native code }
// resolve with this.num*2 after the 1 second
setTimeout(() => resolve(this.num * 2), 1000); // (**)
}
}
new Promise(resolve => resolve(1))
.then(result => {
return new Thenable(result); // (*)
})
.then(alert); // shows 2 after 1000ms
https://masteringjs.io/tutorials/fundamentals/thenable
https://exploringjs.com/es6/ch_promises.html#sec_chaining-promises
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
最新文章
- 2Struts2基础----青软S2SH(笔记)
- openjudge6047分蛋糕[DP]
- 【leetcode】Wildcard Matching(hard) ★ 大神太牛了
- 在CentOS 6.6下安装与配置mysql
- 请求webservice接口的某方法数据
- mysql 函数,关键字,特性
- mysql 中间件 分析
- Spring 对JDBC的支持(JdbcTemplate)
- 解决Access restriction问题
- 传统flv网页视频(flvplayer)--兼容
- java读取txt文件内容
- 【★】致全球第一批全帧3D游戏!
- ActiveMQ使用的设计模式
- mysql 给表和字段加注释
- Android动态添加Device Admin权限
- Linux 第三天
- maven的传递性依赖
- 使用GMap.NET类库,实现地图轨迹回放。(WPF版)
- 高性能JavaScript(加载和执行)
- JSP中解决中文乱码