Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从低级嵌套函数到高级嵌套函数获取数据?_Javascript_Nested_Es6 Promise - Fatal编程技术网

Javascript 如何从低级嵌套函数到高级嵌套函数获取数据?

Javascript 如何从低级嵌套函数到高级嵌套函数获取数据?,javascript,nested,es6-promise,Javascript,Nested,Es6 Promise,我在从低级嵌套函数到高级嵌套函数获取数据时遇到问题,假设所需数据是异步的,我尝试了回调,但没有成功,承诺也没有给我正确的结果-我做错了什么吗?我是node.js环境的新手 代码: var数组=[1,2,3,4,5,6,7,8,9,10]; 对于(var i=0;i{ setTimeout(time=>resolve('Tsomedata'),2000); }); } 函数afunc2(){ 返回新承诺((解决、拒绝)=>{ setTimeout(time=>resolve('Asomedata'

我在从低级嵌套函数到高级嵌套函数获取数据时遇到问题,假设所需数据是异步的,我尝试了回调,但没有成功,承诺也没有给我正确的结果-我做错了什么吗?我是node.js环境的新手

代码:

var数组=[1,2,3,4,5,6,7,8,9,10];
对于(var i=0;i


你知道怎么解决吗?

老实说,我不知道你到底想做什么。JSFIDLE有很多错误,我很难判断其意图

然而,有一种方法可以通过将这些函数移出循环并使用
承诺来简化这一过程。这可能不完全是你所期望的,但我希望它能给你一些依据:

var数组=[1,2,3,4,5,6,7,8,9,10];
var array_length=array.length;
var承诺=[];
console.clear();
函数tfunc1(){
promises.push(tfunc2());
}
函数afunc1(){
promises.push(afunc2());
}
函数tfunc2(){
返回新承诺((解决、拒绝)=>{
setTimeout(time=>resolve('Tsomedata'),2000);
});
}
函数afunc2(){
返回新承诺((解决、拒绝)=>{
setTimeout(time=>resolve('Asomedata'),1000);
});
}
对于(var i=0;i{
//Tsomedata和Asomedata都可以在“数据”数组中找到。
控制台日志(数据);

});谢谢!这是非常有帮助的,我将尝试实施这一特定的路线,看看它是否会起作用。
var array = [1,2,3,4,5,6,7,8,9,10];

for (var i = 0; i < array.length; i++)
{
    if (array[i] == '3'){
        function tfunc1(){
            function tfunc2(){
                return new Promise(function(resolve, reject){
                    setTimeout(function(){ // lets suppose that there is some async work here
                        var tsomeData = 'TsomeData';
                        resolve(tsomeData);
                        reject(console.log('tfunc4 ERROR'));
                    }, 2000);
                });
            }
                    // promises:
                    tfunc2().then(function(tsomeData)
                    {
                        return afunc2(tsomeData);
                    }).then(function({tsomeData, asomeData})
                    {
                        return after(tsomeData, asomeData);
                    }).catch(function()
                    {
                        console.log(err);
                    });
        }
    }

    if (array[i] == '6'){
        function afunc1(){
            function afunc2(tsomeData){
                return new Promise(function(resolve, reject){
                    setTimeout(function(){ // lets suppose that there is some async work here
                        var asomeData = 'AsomeData';
                        resolve({tsomeData : tsomeData, asomeData : asomeData});
                        reject(console.log('afunc4 ERROR'));
                    }, 2000);
                });
            }
        }
    }

    // I need to work with tsomeData here - how to pass it here?
    // I need to work with asomeData here - how to pass it here?

    // tsomeData + asomeData mutating:
    function after(tsomeData, asomeData){
        return new Promise(function(resolve, reject){
            setTimeout(function(){ // lets suppose that there is some async work here
                var newData = tsomeData + asomeData;
                resolve(console.log(newData));
                reject(console.log('after ERROR'));
            }, 2000);
        });
    }
}