-1

This is the code I'm using. It doesn't work because client.getPosts is async. So my question is, how can I make it work? client.getPosts is from the wordpress npm module so I cannot change it.

// FUNCTIONS

function getAllPosts() {
    return client
        .getPosts( {type: 'post', status : 'publish', number : 222} , ['title','id'] , (error, posts) => {
        return posts
            .map((item) => {
            return item.title
            })
        })
}


// MAIN

console.log(getAllPosts());
Dany M
  • 113
  • 2
  • 2
  • 9
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Pablo Lozano Jul 07 '17 at 10:32
  • I sugest you using lamba functions so you can do: somFunction = () => client.getPost(config) and then using promises when calling the function – Òscar Raya Jul 07 '17 at 10:35
  • @arracso any way you could write me literally what I need to write? I've been trying to understand promises for a long time but still dont understand how to use them, especially in this context where I'm using a module. – Dany M Jul 07 '17 at 10:47
  • promises is just a function that executes with the result of another function. I don't know how getPosts works, but if it works like an ajax requests then instead of passing a function to getPosts you can use a promise, in ajax: `$.ajax(/*config*/).done(/*here do whatever you whant woth the response*/)´ or you can use other promises like `.fail()`, `.then()`, `.error()`.. So the best option can be to have the function with his configuration of getPost and name it just like i done. Then you may use the function with the promises. (I cannot explain me better I new to promises to) – Òscar Raya Jul 07 '17 at 11:28

2 Answers2

1

You can simply pass the callback function to getAllPosts.

getAllPosts(cb){
return client
    .getPosts( {type: 'post', status : 'publish', number : 222} , ['title','id'] , (error, posts) => {
    cb(posts
        .map((item) => {
        return item.title
        }))
    })
}

getAllPosts(function(posts){
   console.log(posts)
})
Armen Hajian
  • 26
  • 1
  • 3
  • Thanks, that works. I don't really understand it though. In what order are things happening? – Dany M Jul 07 '17 at 12:00
  • You're welcome. When you call the `getAllPosts` it calls to async `getPosts` function and when the result comes from the server the callback function for `getPosts` fires : `.getPosts({something}, (err, posts)=>{})` The `(err, posts)=>{}` is the callback function for `.getPosts`, in which we'll call to OUR callback function `cb` and pass the result to it. For good understanding please read this [link](http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/) – Armen Hajian Jul 09 '17 at 10:28
0

in .getPosts() method you are using 3 params an object an array and a function referance.

The point is you are sending a referance to that function. You should execute that functon after your process end. but your code line won't wait youır process end.