How to call a function inside bluebirdjs promise

How to call a function inside bluebirdjs promise



I am trying to get a promise function with bluebirdjs. but all attempts falling my way because maybe I don't know what I am doing...?



I want to get files locations, then download the files one after the other then push to an array.


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)
);



When I call the return this.getLocation(file)... I get the following error
bluebird.js:1545 Unhandled rejection TypeError: Cannot read property 'getLocation' of undefined ....bluebird.js


error
bluebird.js:1545 Unhandled rejection TypeError: Cannot read property 'getLocation' of undefined ....bluebird.js



Edit part of the code am using now!


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'));






Try an arrow function for the Promise: Promise.map(files, (file) => ). See Javascript “this” pointer within nested function.
– Xufox
Aug 24 at 4:06



Promise.map(files, (file) =>


)





Why are you calling the getLocation function as member of this value?
– undefined
Aug 24 at 4:08


getLocation


this





@undefined, my aim is to get the file, where should I call the getLocation?
– Obasi Obeny Oj
Aug 24 at 4:09





The function is hosted in that context. You can simply code: return getLocation(file);
– undefined
Aug 24 at 4:10


return getLocation(file);





@undefined You can assume that getLocation and fileFunction are both methods of some outer object or class, so this.getLocation is correct. getLocation() by itself wouldn’t be valid syntax.
– Xufox
Aug 24 at 4:11



getLocation


fileFunction


this.getLocation


getLocation()




1 Answer
1



this.getLocation(file) does not work because you've lost the value of this because you're inside a Promise.map() callback. Remember, that every normal function call in Javascript changes the value of this unless you specifically control the value of this.


this.getLocation(file)


this


Promise.map()


this


this



You can fix that part of the issue with a simple arrow function for your callback like 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;
);



This assumes that this.getLocation(file) returns a promise that resolves to the location value. Are you sure it does that? It looks like there may be more to your problem than just that first error you ran into.


this.getLocation(file)



And, after a side conversation, you also need to fix getLocation() to return a promise that resolves to the desired URL. Looking in the firebase Javascript doc, it appears that getDownloadURL() already returns a promise that resolves to the desired URL. So, you can just return that promise and let Promise.map() manage the results for you.


getLocation()


getDownloadURL()


Promise.map()


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




And, then you would use it all like this:


obj.fileFunction(fileArray).then(locations =>
console.log(locations);
).catch(err =>
console.log(err);
);





This works, except that, I think the this.getLocation(file) isn't called in an async manner. The this.img_array array files index are not the same as in the files array. this.files=[0: "xngb", 1: "bdje"] but in the downloaded urls I get this.img_array=[0: "...bdje", 1: "...xngb"]. How do I make sure the getLocation(file) functions works async? thanks
– Obasi Obeny Oj
Aug 24 at 5:41


this.getLocation(file)


this.img_array


files


this.files=[0: "xngb", 1: "bdje"]


this.img_array=[0: "...bdje", 1: "...xngb"]





@ObasiObenyOj - What do you mean "how do I make sure the getLocation(file) is called async"? The implementation of getLocation() either is async or it's not. It isn't the caller that determines that. It's the implementation of getLocation() that determines that and what it calls and what it returns (it should be returning a promise that resolves with the desired value if it's async).
– jfriend00
Aug 24 at 5:44



getLocation(file)


getLocation()


getLocation()





@ObasiObenyOj - Promise.map() will maintain order of the items for you. Trying to push items into the array yourself with this.img_array.push(url) is a mistake. You should get the resolved values from Promise.map() and then they will be in the right order.
– jfriend00
Aug 24 at 5:47


Promise.map()


this.img_array.push(url)


Promise.map()





getLocation(file) return this._storage.ref(file).getDownloadURL().subscribe(url => // console.log(url) this.img_array.push(url) ); is my get location function. As I was trying to get the resulting this.img_array files index to match the index of the files in the files array. thanks
– Obasi Obeny Oj
Aug 24 at 5:47


getLocation(file) return this._storage.ref(file).getDownloadURL().subscribe(url => // console.log(url) this.img_array.push(url) );


this.img_array


files





@ObasiObenyOj - Does getLocation() return a promise? I have no idea what this._storage.ref(file).getDownloadURL().subscribe(...) is so I can't look it up in any doc to learn more about it.
– jfriend00
Aug 24 at 5:48



getLocation()


this._storage.ref(file).getDownloadURL().subscribe(...)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế