using promises in node.js to create and compare tw

2019-01-20 20:36发布

问题:

I needed to compare two arrays the first one a couple of filenames from a database, the second one a list of files I already downloaded to my client. The Idea was to load whatever files are missing on the client. As the reading via fswas two slow, I tried using Promises to wait for one function to finish before the next starts. But somehow I got lost... My code so far:

let filesIneed = [];
let filesIhave = [];
let filesToFetch = [];
getLocalFiles().then(getFilesIneed).then(getfilesToRetreive);

function getLocalFiles() {
    fs.readdir(localPath, (err, files) => {
        files.forEach(file => {
                filesIhave.push(file)
        });
    })
    return Promise.all(filesIhave);
}

function getFilesIneed () {
    for (let x of docs) {//this is my JSON
        filesIneed.push(y.NameOfFileIShouldHave);
        }
    }
    return Promise.all(filesIneed);
}

function getfilesToRetreive() {
    filesToFetch = _.difference(filesIneed, filesIhave);
    return Promise.all(filesToFetch);
}


console.log(filesToFetch);

I do get the first and second array ("filesIneed" and "filesIhave"), but difference is always empty. So maybe I just mangled up the Promises, as this concept is completely new to me and I'm aware I only understood half of it.

回答1:

This is completely wrong. You cannot run Promise.all on an array of filenames. You can only run it on an array of promises.

There is also no need to push every element of an array one at a time to an empty array just to return that array when you already have that array in the first place.

You cannot use promises to compare two arrays. You can use lodash to compare two arrays in a then handler of a promise, that resolves to an array.

If you want to get a promise of file names from the fs.readdir then use one of the following modules:

  • https://www.npmjs.com/package/mz
  • http://bluebirdjs.com/docs/api/promise.promisifyall.html
  • https://www.npmjs.com/package/fs-promise
  • https://www.npmjs.com/package/fs-promised

Also don't use global variables for everything because you will have problems with any concurrency.

Also, read about promises. Without understanding how promises work you will not be able to guess a correct way of using them. Even looking at some working code examples can help a lot and there are a lot of questions and answers on stack Overflow about promises:

  • promise call separate from promise-resolution
  • Q Promise delay
  • Return Promise result instead of Promise
  • Exporting module from promise result
  • What is wrong with promise resolving?
  • Return value in function from a promise block
  • How can i return status inside the promise?
  • Should I refrain from handling Promise rejection asynchronously?
  • Is the deferred/promise concept in JavaScript a new one or is it a traditional part of functional programming?
  • How can I chain these functions together with promises?
  • Promise.all in JavaScript: How to get resolve value for all promises?
  • Why Promise.all is undefined
  • function will return null from javascript post/get
  • Use cancel() inside a then-chain created by promisifyAll
  • Why is it possible to pass in a non-function parameter to Promise.then() without causing an error?
  • Implement promises pattern
  • Promises and performance
  • Trouble scraping two URLs with promises
  • http.request not returning data even after specifying return on the 'end' event
  • async.each not iterating when using promises
  • jQuery jqXHR - cancel chained calls, trigger error chain
  • Correct way of handling promisses and server response
  • Return a value from a function call before completing all operations within the function itself?
  • Resolving a setTimeout inside API endpoint
  • Async wait for a function
  • JavaScript function that returns AJAX call data
  • try/catch blocks with async/await
  • jQuery Deferred not calling the resolve/done callbacks in order
  • Returning data from ajax results in strange object
  • javascript - Why is there a spec for sync and async modules?
  • Return data after ajax call success