Get promise result then pass the result into a variable

417 views
Skip to first unread message

Abdullah Al Naiem

unread,
Jan 26, 2022, 5:32:49 PM1/26/22
to Chromium Extensions
Here's my code and I want to assign promise result into a variable then do the same thing again
function ok(x){
    return new Promise((resolve, reject) => {
      resolve(x);
    });
}

var num = 0
ok(5).then((x) => { num = x+2 });
ok(num).then((x) => { num = x+5 });
ok(num).then((x) => { console.log(x) });

Abdullah Al Naiem

unread,
Jan 26, 2022, 5:34:52 PM1/26/22
to Chromium Extensions, Abdullah Al Naiem
I know my code is wrong, but my approach is something like this code

Ibrahim

unread,
Jan 27, 2022, 5:46:32 AM1/27/22
to Chromium Extensions, naie...@gmail.com
You could use await to wait for a promise to end.:

function ok(x){
    return new Promise((resolve, reject) => {
      resolve(x);
    });
}


(async function() {
   var num = 0;
   num = await ok(5) + 2;
   num = await ok(num) + 5;
   console.log(await ok(num));
})()

Reply all
Reply to author
Forward
0 new messages