What happens if i reject / resolve multiple times in Kriskowal's q? What happens if i reject / resolve multiple times in Kriskowal's q? node.js node.js

What happens if i reject / resolve multiple times in Kriskowal's q?


Since promises can only resolve once (to either fulfilled or rejected), the first resolution wins and any further calls will be ignored. From the docs:

In all cases where a promise is resolved (i.e. either fulfilled or rejected), the resolution is permanent and cannot be reset. Attempting to call resolve, reject, or notify if promise is already resolved will be a no-op.

Should i avoid to call reject/resolve multiple times?

You can even design your application letting two methods "race" against each other to resolve a deferred, but in general it should be avoided to reduce confusion of a reader.


original post here

see github gist: reuse_promise.js

/*reuse a promise for multiple resolve()s since promises only resolve once and then never again*/import React, { useEffect, useState } from 'react'export default () => {        const [somePromise, setSomePromise] = useState(promiseCreator())            useEffect(() => {                somePromise.then(data => {                        // do things here                        setSomePromise(promiseCreator())        })            }, [somePromise])}const promiseCreator = () => {    return new Promise((resolve, reject) => {        // do things        resolve(/*data*/)    })}