Wednesday, June 5, 2024
1
rated 0 times [  4] [ 3]  / answers: 1 / hits: 19383  / 7 Years ago, thu, september 7, 2017, 12:00:00

I was wondering if there is a way to get the second resolve value (test2) without returning arrays or JavaScript objects.





function testFunction() {
return new Promise(function(resolve, reject) {
resolve(test1, test2);
});
}

async function run() {
var response = await testFunction();
console.log(response); // test1
}

run();




More From » asynchronous

 Answers
53

You can pass only one item. But starting from ES6 there is a good feature called Array Destructuring.



Return an array and you can leave the properties assignment under the hood.





function testFunction() {
return new Promise(function(resolve, reject) {
resolve([ test1, test2] );
});
}

async function run() {

const [firstRes, secondRes] = await testFunction();

console.log(firstRes, secondRes);

}

run();




[#56556] Monday, September 4, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monica

Total Points: 308
Total Questions: 102
Total Answers: 109

Location: Saudi Arabia
Member since Sat, Aug 20, 2022
2 Years ago
;