1

I would like to do something like this

function doSomething(aPromise) {
    if (/*xxx*/) {
        // doSomethingElse
    } else {
        return aPromise
            .then(doSomethingMore)
    }
}

However, if i do this, aPromise will always be executed when calling doSomething. How do i lazy execute it until it get called

0

1 Answer 1

3

Try using aPromise as a function , returning Promise value from calling aPromise() at else block

function aPromise() {
  return Promise.resolve(/* `Promise` return value */)
}

function doSomething(aPromise) {
    if (/*xxx*/) {
        // doSomethingElse
    } else {
        // call `aPromise()` 
        return aPromise()
            .then(doSomethingMore)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.