How to split an array received in a Promise [on hold]

问题内容:

In the below code, I receive data in the ‘then()’ branch as hown in the code below. The ‘data[0] is an array contains the following:

203375 7336 5397 7283 0 10414 0

I want to have only the 2nd and the 3rd value of that array. therefore i used

data[0].split(' ')

but when I run the code I receive the errors posted below

please let me know how to split an array passed to the ‘ then()’ of a promise

results

Promise.all(promises).then((data) => {

  console.log('statm: ' + data[0]);
  console.log('RSS: ' + data[0].split(' ') + ' Pages');
  //console.log('Shared: ' + data[0].split(' ')[2]+ ' pages');



  console.log('PageSize: ' + data[1] / data[2]);

}, err => {
  callback(err);

});

error:

(node:13806) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: data[0].split is not a function
(node:13806) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

问题评论:

1  
If data[0] is an array then you can simply do data[0][1] and data[0][2]
2  
Promises are entirely irrelevant here. data[0] is simply not what you think it is. console.log(data[0]) – what exactly does that give you?
    
why are you using both callback and promise? you can handle error like this : promise.all().then().catch()
    
can you share what is inside the “data”

原文地址:

https://stackoverflow.com/questions/47753285/how-to-split-an-array-received-in-a-promise

添加评论

友情链接:蝴蝶教程