简体   繁体   中英

Convert then to async await javascript

I am checking the library promise-retry

and I can see that they do the following

promiseRetry(function (retry, number) {     
    return doSomething()
    .catch(retry);
})
.then(function (value) {
    // ..
}, function (err) {
    // ..
});

I am using async/await throughout the project, so I have converted it to

const resp = await promiseRetry((retry, number) => doSomething().catch(retry));

So this will work to get the value when the retrying works, but what about cases of failure? In the original code they have function(err) , but how can this be replicated? It would have been possible if it was a .catch(err) , but it's more of callback to the then function.

The answer depends on what the code in the ... of the original is. If the original is:

promiseRetry(function (retry, number) {     
    return doSomething()
    .catch(retry);
})
.then(function (value) {
    // ...using `value` here...
}, function (err) {
    // ...using `err` here...
});

then because that code is using the two-argument version of then , the near-equivalent using async / await is a bit of a pain:

let failed false;
let value;
try {
    value = await promiseRetry((retry, number) => doSomething().catch(retry));
} catch (err) {
    failed = true;
    // ...using `err` here...
}
if (!failed) {
    // ...using `value` here...
}

but with more context it might well be possible write something less cumbersome.

That said, most people would probably write this instead:

try {
    const value = await promiseRetry((retry, number) => doSomething().catch(retry));
    // ...using `value` here...
} catch (err) {
    // ...using `err` here...
}

The difference is whether the catch block catches errors during ...using `value` here... . It doesn't in the original but does in the "most people would probably write" above, which is closer to:

promiseRetry(function (retry, number) {     
    return doSomething()
    .catch(retry);
})
.then(function (value) {
    // ...using `value` here...
})
.catch(function (err) {
    // ...using `err` here...
});

Not having the code in the rejection handler handle rejections from the code in the fulfillment handler definitely makes sense sometimes, and perhaps particularly in a retry scenario: If the original action succeeded but the code handling the result of it fails, you probably don't want to retry the original action. That may well be why the original code used the two-argument then rather than then / catch .

In Javascript if you use the "await" keyword and a error occured it will be turned into an classic exception.

Async/await in JS

If it's an error, the exception is generated — same as if throw error were called at that very place.

Edit: So you need to use the try/catch mechanism

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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