0

Hello I have a function that goes to the database and returns array of objects like so:

function findAlbumImages(){
   remote.findAlbum.then(
       res =>{

       })    
}

but I want to call this from another function and assign that res to array collection like so:

let newArray = findAlbumImages();

Is there a way to do this?

Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
ivaylo
  • 811
  • 2
  • 9
  • 25
  • 1
    you can return promise from findAlbumImages(). use then function on another function. – Sameer Dec 03 '18 at 12:47
  • Sorry in advance, I know this isn't the right place. Ivaylo, you deleted one of your questions before I could answer. Didn't want to waste the effort, here you go: https://jsfiddle.net/SydLambert/hnkL392x/2/ – Syd Lambert Jan 05 '19 at 01:48
  • @ivaylo No problem, happy to help :) Can you please post the link to it, it's not showing up. – Syd Lambert Jan 06 '19 at 17:48
  • @SydLambert https://stackoverflow.com/questions/54046647/bouncing-draggable-ball – ivaylo Jan 07 '19 at 22:00

1 Answers1

4

Sure you can, by using async/await, which is the closest you can get to your desired syntax:

function findAlbumImages() {
  return remote.findAlbum()
}

(async () => {
  let newArray = await findAlbumImages()

  console.log(newArray)
})()
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
  • Hello so this is what it returns {readyState: 1, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}. Any ideas? – ivaylo Dec 03 '18 at 13:03
  • Make sure you're doing `await findAlbumImages()` just like I have in my example. – nicholaswmin Dec 03 '18 at 13:03
  • Yes, thank you can you please gvie me an idea to what it returns without the await, is this a function that it returns? – ivaylo Dec 03 '18 at 13:09
  • I guess i found something about it, thanks anyway. – ivaylo Dec 03 '18 at 13:12