Javascript Promise 中的动态链接 [英] Dynamic Chaining in Javascript Promises

查看:15
本文介绍了Javascript Promise 中的动态链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在 Javascript Promises 中执行动态 链接,一直以来我只看到对调用的硬编码,例如 (promise).then(request/functionName).then(请求/函数名)

How can I perform dynamic chaining in Javascript Promises, all the time I have seen only hardcoding of the calls for eg., (promise).then(request/functionName).then(request/functionName)

推荐答案

一种选择是利用对象的属性以及通过字符串调用它们的能力.

One option is to utilize the properties of objects and the ability to invoke them via strings.

我写了一个小样本这里并发布在下面.

I wrote a small sample Here and posted it below.

这个想法是你有一组你希望在某个命名空间或对象中运行的函数,就像我在myNamespace"中所做的那样:

The idea is that you have the set of functions that you wish to run set in some namespace or object, as I did in 'myNamespace':

myNamespace = {
    "A": function() {return "A Function";},
    "B": function() {return "B Function";},
    "C": function() {return "C Function";}
}

然后你的主要承诺会运行并且以某种方式(通过输入、ajax、提示等)你会得到你想要运行的函数的字符串值,直到运行时才知道:

Then your main promise would run and somehow (via inputs, ajax, prompts, etc.) you would get the string value of the function you want to have run, which isn't known until runtime:

我的主要承诺使用提示来获取用户的来信:

My main promise uses a prompt to get a letter from the user:

var answer = prompt('Starting.  Please pick a letter: A,B,C');
        if(myNamespace[answer] === undefined)
        {
            alert("Invalid choice!");
            reject("Invalid choice of: " + answer);
        }
        else
        {
            resolve(answer);
        }

在接下来的then"中,我使用该值(通过 resolve 函数传递)来调用该函数:

In the next 'then' I use that value (passed via the resolve function) to invoke the function:

.then(function(response) {
        funcToRun = myNamespace[response]();})

最后,我将动态函数调用的结果输出到 html,并使用了一些递归的乐趣来使其更具交互性并证明它是动态的:

Finally, I output to html the result of my dynamic function call and I use some recursive fun to make it more interactive and demonstrate that it is dynamic:

.then(function(){
        document.getElementById('result').innerHTML = funcToRun;})
    .then(function(){
        if(prompt("Run Again? (YES/NO)")==="YES")
        {
            doWork();
        }
    });

myNamespace = {
    "A": function() {return "A Function";},
    "B": function() {return "B Function";},
    "C": function() {return "C Function";}
}

function doWork()
{
    var funcToRun;
    
    new Promise(function(resolve,reject) {
        var answer = prompt('Starting.  Please pick a letter: A,B,C');
        if(myNamespace[answer] === undefined)
        {
            alert("Invalid choice!");
            reject("Invalid choice of: " + answer);
        }
        else
        {
            resolve(answer);
        }
    })
    .then(function(response) {
        funcToRun = myNamespace[response]();})
    .then(function(){
        document.getElementById('result').innerHTML = funcToRun;})
    .then(function(){
        if(prompt("Run Again? (YES/NO)")==="YES")
        {
            doWork();
        }
    });
}

doWork();

<div id="result"></div>

这篇关于Javascript Promise 中的动态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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