Thaina Thaina - 1 year ago 109
Javascript Question

Can we get current promise in then?

I want to have some mechanism to redo then function in promise. Keep doing it until it pass some check

somePromise.then(() => {
if(state == "finish")

// DoSomething async then repeat these codes
// could we do it by [return this;] ?
}).then(() => console.log("finish"));

Answer Source

You can name the function used at .then(), recursively call same function at .then() of doSomethingAsync() call until state == "finish"

// name function used at `.then()` 
somePromise.then(function re(data) {
    if(state == "finish") {
        return data;
    // DoSomething async then repeat these codes
    // call `re` at `.then()` chained to `doSomethingAsync()`
}).then((data) => console.log("finish", data));

See also multiple, sequential fetch() Promise

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download