Should I fulfill my JavaScript promises?

In Node.js , if I do this:

var doNoResolve = true;

function a() {
    return new Promise(resolve => {
        if (doNotResolve) {
            return
        }
        resolve(10);
    });
}

a().then(() => {
    // I don't want this getting fired
});

Is there a memory leak on an incoming request? If I used the plain old callback, everything would have worked just fine if I hadn't performed any callback, but it looks like it might not be ... the name promise itself implies that this is somewhat wrong.

If I had to return a “fake promise” ( return { then: () => {} }) inside function a(), and not a “real promise”, if doNotResolve was true, but that’s a little rude.

- React.js, HTTP , ( , , , , ).

+4
1

, ?

promises , , :

  • then ( , )
  • catch,
  • - finally

:

function a() {
    return new Promise((resolve, reject) => {
        if (doNotResolve) {
            reject(new Error('Oh noes!'));
        }
        resolve(10);
    });
}

Promise , reject, , catch/finally:

a().then(val => {
  console.log('Got data:', val);
}).catch(err => {
  console.error(err);
}).finally(() => {
  console.log('Done!');
});

, promises , , node. , Bluebird , promises, .

+4

Source: https://habr.com/ru/post/1599542/


All Articles