将文本引用替换为Promises返回的数据 [英] Replace text occurrences with data returned by Promises

查看:68
本文介绍了将文本引用替换为Promises返回的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的博客文章:

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]";

现在每次出现 [embed] soundcloud-url [/ embed] 我需要调用他们的API端点 http://api.soundcloud.com/resolve.json?url=soundcloud-url&client_id=my-id ,解析返回的JSON并用我自己的标记替换 [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.

如何我可以使用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


推荐答案

我没有特别使用 bluebird ,但通常一个所有方法包含一系列承诺(或承诺作为参数)。浏览蓝鸟的API文档,他们确实有一个 所有 方法,你可以使用。你需要创建一个数组承诺在你的do / while循环中,然后在最后调用所有

I haven't used bluebird specifically, but there's usually an all method that wraps an array of promises (or takes promises as arguments). A glance at the API documentation for bluebird shows they do have an all method that you can use. You'll want to create an array of promises inside your do/while loop, then call all at the end:

var resultPromises = [];
var m;
do {
    m = re.exec(post);
    if (m) {
        var apiCallUrl = "http://api.soundcloud.com/resolve.json?url=" + m[1] '&client_id=...'
        resultPromises.push(request(apiCallUrl));
    }
} while (m);

// have all promises fulfilled and old embed in post-var replaced
Promise.all(resultPromises).then(function (results) {
    // do stuff.
});

现在,如果您想将原始文本替换为承诺的结果,您需要将匹配也存储在数组中。 然后结果参数将是响应的数组,按照它们最初添加到的顺序数组。所以,你可以这样做:

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 the then 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:

var resultPromises = [];
var matches = [];
var m;
do {
    m = re.exec(post);
    if (m) {
        var apiCallUrl = "http://api.soundcloud.com/resolve.json?url=" + m[1] '&client_id=...'
        resultPromises.push(request(apiCallUrl));
        matches.push(m);
    }
} while (m);

var i = 0;
// have all promises fulfilled and old embed in post-var replaced
Promise.all(resultPromises).then(function (results) {
    // haven't tested this. Will leave as practice for the OP :)
    post = post.replace(matches[i], results[i].body[0].body.title);
    i += 1;
});

这篇关于将文本引用替换为Promises返回的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆