Javascript 如何在bluebirdjs promise中调用函数

Javascript 如何在bluebirdjs promise中调用函数,javascript,bluebird,Javascript,Bluebird,我试图用bluebirdjs获得一个承诺函数。但是所有的尝试都是因为我不知道自己在做什么 我想获得文件的位置,然后一个接一个地下载文件,然后推送到阵列 import * as Promise from 'bluebird'; fileFunction(files){ Promise.map(files, function(file) { // Promise.map awaits for returned promises as well. //Promise.delay(1

我试图用bluebirdjs获得一个承诺函数。但是所有的尝试都是因为我不知道自己在做什么

我想获得文件的位置,然后一个接一个地下载文件,然后推送到阵列

import * as Promise from 'bluebird';
fileFunction(files){
  Promise.map(files, function(file) {
    // Promise.map awaits for returned promises as well.
    //Promise.delay(1000);
     return this.getLocation(file); 
    },
  ).then((file) => {
      console.log('done')
    });

}

getLocation(file){
 if(file){
  return this._storage.ref(file).getDownloadURL().subscribe(url => 
      this.img_array.push(url)
  );
 }
当我调用returnthis.getLocation(文件)时。。。我得到以下
错误
bluebird.js:1545未处理的拒绝类型错误:无法读取未定义的…bluebird.js的属性“getLocation”

编辑我现在使用的部分代码

 fileFunction(files){
    return Promise.map(files, file => {
       return this.getDownloadUrl(file); 
    }).then(locations => {
        // add these locations onto this.img_array
        this.img_array.push(...locations);
        console.log('done');
        return locations;
    });
  }


getFiles(e): Promise<any>{
  this.outPutFiles = e;

 this.fileFunction(this.outPutFiles).then(locations => {
  locations.map((arr) => arr.subscribe((files) => this.downloadUrls.push(files)));
  }).catch(err => {
      console.log(err);
  });
}


getDownloadUrl(file){
  if(file){
    return this._storage.ref(file).getDownloadURL();
   } else {
    return Promise.reject(new Error('No file passed to getLocation'));
   }
}
fileFunction(文件){
return Promise.map(文件,文件=>{
返回此.getDownloadUrl(文件);
})。然后(位置=>{
//将这些位置添加到此.img_数组中
这个.img_数组.push(…位置);
console.log('done');
返回地点;
});
}
getFiles(e):承诺{
this.outPutFiles=e;
this.fileFunction(this.outPutFiles).then(locations=>{
locations.map((arr)=>arr.subscribe((文件)=>this.downloadURL.push(文件));
}).catch(错误=>{
控制台日志(err);
});
}
getDownloadUrl(文件){
如果(文件){
返回此。_storage.ref(文件).getDownloadURL();
}否则{
return Promise.reject(新错误('No file passed to getLocation'));
}
}
此.getLocation(文件)
不起作用,因为您在
Promise.map()回调中,因此丢失了此
的值。请记住,Javascript中的每个普通函数调用都会更改
this
的值,除非您专门控制
this
的值

您可以通过一个简单的回调箭头函数来解决这部分问题,如下所示:

fileFunction(files){
  return Promise.map(files, file => {
     return this.getLocation(file); 
  }).then(locations => {
      // add these locations onto this.img_array
      this.img_array.push(...locations);
      console.log('done');
      return locations;
  });
}
obj.fileFunction(fileArray).then(locations => {
    console.log(locations);
}).catch(err => {
    console.log(err);
});
这假设
This.getLocation(文件)
返回解析为位置值的承诺。你确定是这样的吗?看起来你的问题不仅仅是你遇到的第一个错误


此外,在一次旁白之后,您还需要修复
getLocation()
,以返回解析为所需URL的承诺。查看firebase Javascript文档,似乎
getDownloadURL()
已经返回解析为所需URL的承诺。因此,您可以返回该承诺,并让
promise.map()
为您管理结果

getLocation(file){
 if(file){
  return this._storage.ref(file).getDownloadURL();
 } else {
  return Promise.reject(new Error("No file passed to getLocation"));
 }
}

然后,你会像这样使用它:

fileFunction(files){
  return Promise.map(files, file => {
     return this.getLocation(file); 
  }).then(locations => {
      // add these locations onto this.img_array
      this.img_array.push(...locations);
      console.log('done');
      return locations;
  });
}
obj.fileFunction(fileArray).then(locations => {
    console.log(locations);
}).catch(err => {
    console.log(err);
});

尝试为Promise使用箭头函数:
Promise.map(files,(file)=>{
..
})
。请参阅。为什么要作为
值的成员调用
getLocation
函数?@undefined,我的目标是获取文件,我应该在哪里调用getLocation?该函数位于该上下文中。您只需编写代码:
返回getLocation(文件)
@undefined您可以假设
getLocation
fileFunction
都是某些外部对象或类的方法,因此
这个.getLocation
是正确的
getLocation(){}
本身不是有效的语法。这是有效的,但我认为
This.getLocation(文件)
不是以异步方式调用的。
this.img_数组
数组文件索引与
文件
数组中的索引不同
this.files=[0:“xngb”,1:“bdje”]
但是在下载的URL中我得到了
this.img_数组=[0:“…bdje”,1:“…xngb”]
。如何确保getLocation(file)函数异步工作?thanks@ObasiObenyOj-什么是“如何确保
getLocation(文件)
被称为异步”?
getLocation()
的实现要么是异步的,要么不是。这不是由调用方决定的。它是
getLocation()
的实现,它决定了它调用的内容和返回的内容(如果是异步的,它应该返回一个与所需值解析的承诺)。@ObasiObenyOj-
promise.map()
将为您维护项目的顺序。尝试自己使用
this.img\u array.push(url)
将项目推入数组是错误的。您应该从
Promise.map()
获得解析值,然后它们将按正确的顺序排列。
getLocation(file){返回此值。_storage.ref(file).getDownloadURL().subscribe(url=>//console.log(url)this.img_array.push(url))}
是我的get location函数。当我试图获取结果
this.img_数组
files索引以匹配
files
数组中文件的索引时。thanks@ObasiObenyOj-是否
getLocation()
返回承诺?我不知道这是什么。_storage.ref(file).getDownloadURL().subscribe(…)
所以我无法在任何文档中查找它来了解更多信息。