0

ONLY in the context of promises and callbacks, does the following make sense?

static getDefaultAdminHeader(_this) {
  return new Promise((resolve, reject) => {
    [NodeJS fs module].readFile([NAME OF FILE], (err, fd) => {
      resolve(fd);
    });
 });
}

In other words: Does the promise above do anything more then simply add an extra callback? Or is there a programmatic plus to this structure?

  • 3
    You should `if (err) reject(err);`, otherwise this could just hang forever. But otherwise there's nothing particularly wrong with this if you want to use promises rather than callbacks in the rest of your codebase. Depending on the libraries you're using you may already have a promisify utility function for Node-style callbacks. See e.g. https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises – jonrsharpe Aug 20 '17 at 09:55
  • @jonrsharpe, I think you should add this as an answer. – Raghav Garg Aug 20 '17 at 09:58
  • What do you mean by "*anything more then simply add an extra callback*"? A plus, compared to what else? – Bergi Aug 20 '17 at 10:41
  • @Bergi - I'll try to rephrase (I find the question somewhat confusing, and I'm the one asking it): If I'm calling an asynchronous NodeJS function such as [fs].readFile, does adding a promise help with anything, or is the callback provided to the 'readFile' enough of a promise on its own. Am I adding or lessening work by wrapping the asynchronous method in a promise? Or am I simply adding a promise to handle a promise? – Rama Schneider Aug 21 '17 at 10:48
  • @RamaSchneider Maybe have a look at [what promises add over callbacks](https://stackoverflow.com/a/22562045/1048572)? Not sure if that answers your question. Promises lessen the programmer's work significantly. – Bergi Aug 21 '17 at 13:22

1 Answers1

0

from your question,

promise can be nested as many layers as you want, but your code will look dirty.

usually, we should not do nested promise, or mixed promise and callback.

from your code, there are many solution to remove callback.

  1. use fs promise module instead, e.g., https://www.npmjs.com/package/fs-extra
  2. use standard fs with promisifyAll function from bluebird library
Alongkorn
  • 3,968
  • 1
  • 24
  • 41
  • Maybe I can clarify my question: Function 'A' calls Function 'B' which creates promise function 'C' which creates asynchronous function 'D'. C returns to B (and B to A, that part really is necessary) the result of D. Am I needlessly using a promise wrapper around an asynchronous function? Or is there a server-side benefit to doing this? (Am I making sense?) – Rama Schneider Aug 20 '17 at 10:56
  • @RamaSchneider you can create function A, B, C, D. All of them return promise. and then use A.then(B).then(C).then(D) in main function – Alongkorn Aug 20 '17 at 11:01
  • in the command section, i cannot preformat my code, but you may get an idea – Alongkorn Aug 20 '17 at 11:02
  • @RamaSchneider The promise is not needless but in fact necessary, as otherwise you cannot `return` an asynchronous result from a function. You'd have to pass in callbacks instead. – Bergi Aug 20 '17 at 11:25