I've got a blog-post like this:
var post ="## Cool Post [embed]https://soundcloud.com/wonnemusik/teaser-schotone-dear-thomson-button-remix[/embed]"
+ "Here comes another one [embed]https://soundcloud.com/straightech-bonn-podcast/straightech-and-friends-7-oscar-ozz[/embed]";
Now for every occurrence of [embed]soundcloud-url[/embed]
I need to call their API endpoint http://api.soundcloud.com/resolve.json?url=soundcloud-url&client_id=my-id
, parse the returned JSON and replace the [embed]
with my own markup.
How can I use this using Promises?
var post ="## Cool Post [embed]https://soundcloud.com/wonnemusik/teaser-schotone-dear-thomson-button-remix[/embed]"
+ "Here comes another one [embed]https://soundcloud.com/straightech-bonn-podcast/straightech-and-friends-7-oscar-ozz[/embed]"
var re = /\[embed\](.*)\[\/embed\]/gm;
var m;
do {
m = re.exec(post);
if (m) {
var apiCallUrl = "http://api.soundcloud.com/resolve.json?url=" + m[1] '&client_id=...'
request(apiCallUrl).then(function(body) {
var trackinfo = JSON.parse(body[0].body)
return trackinfo.title
}
}
} while (m);
// have all promises fulfilled and old embed in post-var replaced
I haven't used
bluebird
specifically, but there's usually anall
method that wraps an array of promises (or takes promises as arguments). A glance at the API documentation for bluebird shows they do have anall
method that you can use. You'll want to create an array of promises inside your do/while loop, then callall
at the end:Now, if you want to replace the original text with the results of the promise, you'll need to store the matches in an array as well. The
results
argument to thethen
will be an array of the responses, in the order in which they were originally added to the array. So, you can do something like:You can use this
replace
function that deals with asynchronous callbacks: