栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

许诺可以对onFulfilled有多个参数吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

许诺可以对onFulfilled有多个参数吗?

我在这里遵循规范,不确定是否允许使用多个参数调用onFulfilled。

不,在promise构造函数中,仅第一个参数将被视为分辨率值。您可以使用诸如对象或数组之类的复合值进行解析。

我不在乎任何特定的promise实现如何实现,我希望严格遵循w3c规范中的promise。

那就是我认为你错了的地方。该规范被设计为最小化,旨在在Promise库之间进行互操作。这个想法是要有一个子集,例如DOM期货可以可靠地使用,而库可以使用。Promise实现暂时会执行您要求的操作

.spread
。例如:

Promise.try(function(){    return ["Hello","World","!"];}).spread(function(a,b,c){    console.log(a,b+c); // "Hello World!";});

与bluebird。如果需要此功能,一种解决方案是对其进行填充。

if (!Promise.prototype.spread) {    Promise.prototype.spread = function (fn) {        return this.then(function (args) { return Promise.all(args); // wait for all        }).then(function(args){         //this is always undefined in A+ complaint, but just in case return fn.apply(this, args);         });    };}

这使您可以:

Promise.resolve(null).then(function(){    return ["Hello","World","!"]; }).spread(function(a,b,c){    console.log(a,b+c);    });

带着原生的诺言轻松自在地摆弄。或使用在浏览器中现在很普遍的价差(2018):

Promise.resolve(["Hello","World","!"]).then(([a,b,c]) => {  console.log(a,b+c);    });

或等待:

let [a, b, c] = await Promise.resolve(['hello', 'world', '!']);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495365.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号