Mar*_*oer 12 javascript asynchronous async-await es6-promise
我想知道是否有办法获得第二个resolve值(test2)而不返回数组或JavaScript对象.
function testFunction() {
return new Promise(function(resolve, reject) {
resolve("test1", "test2");
});
}
async function run() {
var response = await testFunction();
console.log(response); // test1
}
run();Run Code Online (Sandbox Code Playgroud)
Sur*_*yan 21
您只能传递一个项目.但从这里开始ES6有一个很好的功能叫做阵列解构.
返回一个数组,您可以将属性分配保留在引擎盖下.
function testFunction() {
return new Promise(function(resolve, reject) {
resolve([ "test1", "test2"] );
});
}
async function run() {
const [firstRes, secondRes] = await testFunction();
console.log(firstRes, secondRes);
}
run();Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7799 次 |
| 最近记录: |