简体   繁体   English

在回调中设置全局变量

[英]Setting global variable in callback

Tried to simplify my code to show my problem. 试图简化我的代码以显示我的问题。

var rp = require('request-promise');
var ids = [];

runmyFunction();
runmyFunction();

function runmyFunction() {
    var id = 5;
    console.log("runmyFunc:  "+ids);
    if (ids.indexOf(id)==-1){
       myFunction(id);
    }
}

function myFunction(id) {
    var options = {
        uri: 'someURI'
        , headers: {
            'User-Agent': 'Request-Promise'
        }
        , json: true
    };
    rp(options)
        .then(function (response) {
            ids.push(5);
            console.log("myFunc: "+ids);
        })
        .catch(function (err) {
           console.log(err);
        });
}

Basically I have this function runmyFunction , which should only execute myFunction if there is not the id 12345 in it. 基本上,我有此函数runmyFunction ,如果其中没有ID 12345,则应仅执行myFunction So if I run it 10 times and the 11th the id 12345 is returned, it should stop running the function. 因此,如果我运行10次并且11号返回ID 12345,它应该停止运行该函数。 The problem is that the id never gets pushed in the array even if the request succeeded. 问题是即使请求成功,id也不会被推送到数组中。

Similar problems had something to do with the request being asynchronous. 类似的问题与请求异步有关。 Is this the cause in my code as well? 这也是我代码中的原因吗?

//edit I played around with it and edited the code. // edit我玩过它并编辑了代码。 Current code logs this into the console: 当前代码将此记录到控制台中:

runmyFunc:  
runmyFunc:  
myFunc:5
myFunc:5,5

So it is because it is asynchronus. 之所以如此,是因为它是异步的。 Any ideas to avoid that? 有什么想法可以避免这种情况吗?

not sure but i think your problem will be solved by using .bind() maybe help : 不确定,但是我认为您的问题将通过使用.bind()解决,可能会有所帮助:

var rp = require('request-promise');
var ids = [];


function runmyFunction() {
    if (ids.indexOf(12345)==-1){
   myFunction();
   }
}

function myFunction() {
    var options = {
    uri: 'someURI'
    , headers: {
        'User-Agent': 'Request-Promise'
    }
    , json: true
};
rp(options)
    .then(function (response,mids) {
        mids.push(response.data.id);
    }.bind(ids))
    .catch(function (err) {
       console.log(err);
    });
}

Thanks to @Shilly i found a solution by myself by adding another array. 感谢@Shilly,我通过添加另一个数组找到了自己的解决方案。

var rp = require('request-promise');

var ids = [];
var idstmp = [];

runmyFunction();
runmyFunction();

function runmyFunction() {
    var id = 5;
    if (ids.indexOf(id) == -1 && idstmp.indexOf(id) == -1) {
        console.log("I was here.");
        idstmp.push(id);
        myFunction(id);
    }
}

function myFunction(id) {
    var options = {
        uri: 'someURI'
        , headers: {
            'User-Agent': 'Request-Promise'
        }
        , json: true
    };
    rp(options)
        .then(function (response) {
            idstmp.splice(idstmp.indexOf(id), 1);
            ids.push(id);
            console.log("myFunc: " + ids);
        })
        .catch(function (err) {
        idstmp.splice(idstmp.indexOf(id), 1);
        });
}

What this does is just adding the ids which are curently 'in use' by the request to another array so it gets block while beeing worked with. 这样做只是将请求当前正在“使用中”的id添加到另一个数组中,这样它在与bee一起工作时会被阻塞。 It is quite a workaround but works for me. 这是一个解决方法,但对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM