I'm having a function returning a promise. In this function, we call a third party vender to send some push notification through their server.
it looks like
apiGetLoggedInUser.then(
user => {
return sendMessage(user.name);
}
)
However the thing is we decided to wait for 3 seconds before we really call this sendMessage function. However we'd prefer not to change sendMessage since it's provided.
I'm wondering how to really do the "wait" part in this scenario since promise is used to remove "sync" operations.
Am I understanding correctly? What shall I do?
The short version:
function wait(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
Example:
async function myFunc(user) {
await wait(3000);
sendMessage(user.name);
}
Insert another promise in the chain which delays the next one:
apiGetLoggedInUser
.then(user => {
return new Promise(resolve => setTimeout(() => resolve(user), 3000));
})
.then(user => sendMessage(user.name))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With