简体   繁体   English

为什么 Promise 设计只返回一个结果?

[英]Why Promise design for only returns one result?

 new Promise((resolve, reject) => { resolve('result1', 'resolve2'); }).then((result1, result2) => { console.info(result1, result2); });

Why Promise returns only one result in JavaScript like in the above code?为什么 Promise 像上面的代码一样在 JavaScript 中只返回一个结果?

I'm sorry for my unclearly describe.我很抱歉我的描述不清楚。 I'm know how to return an object or an array value in resolve.我知道如何在解析中返回对象或数组值。 I'm just curious why Promise design to return only on result.我只是好奇为什么Promise 设计只返回结果。

Promise always returns one value. Promise 总是返回一个值。 But you can do a trick to get as two using Array destructing .但是您可以使用Array destructing做一个技巧来获得两个。 Pass an array or object to the resolve/reject function and using destructing get them as separate variables.将数组或对象传递给resolve/reject函数并使用析构将它们作为单独的变量。

 new Promise((resolve, reject) => { resolve( ['result1', 'resolve2'] ); }).then( ( [result1, result2] ) => { console.info(result1, result2); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM