var mchArr = []; getMchArr((mch_arr) => { // 调用 getMchArr 并在回调函数中处理返回的 mch_arr console.log(mch_arr) mchArr = mch_arr; });等待赋值完成再打印mchArr
时间: 2024-03-08 15:50:13 浏览: 26
如果需要等待 `mchArr` 赋值完成后再打印,可以将打印语句放到回调函数中:
```
var mchArr = [];
getMchArr((mch_arr) => { // 调用 getMchArr 并在回调函数中处理返回的 mch_arr
mchArr = mch_arr;
console.log(mchArr); // 在回调函数中打印 mchArr 的值
});
```
这样,当回调函数执行时,`mchArr` 已经被赋值,可以正确地打印出其值。需要注意的是,在回调函数中处理 `mchArr` 值时,仍然需要考虑异步执行的情况。如果在回调函数中需要对 `mchArr` 进行处理,建议使用 Promise 或 async/await 等方式来处理异步调用。
相关问题
var mchArr = []; getMchArr((mch_arr) => { // 调用 getMchArr 并在回调函数中处理返回的 mch_arr console.log(mch_arr) mchArr = mch_arr; });等待赋值完成再打印mchArr
如果想要在回调函数执行完毕后再打印 `mchArr` 的值,可以将打印操作放在一个函数中,并在回调函数中调用该函数:
```
var mchArr = [];
function printMchArr() {
console.log(mchArr);
}
getMchArr((mch_arr) => { // 调用 getMchArr 并在回调函数中处理返回的 mch_arr
mchArr = mch_arr;
printMchArr(); // 在回调函数中调用打印函数
});
```
这样,在回调函数执行完毕后,会调用 `printMchArr` 函数打印 `mchArr` 的值。需要注意的是,由于 JavaScript 是单线程执行的,如果在回调函数执行之前就调用了 `printMchArr` 函数,那么 `mchArr` 的值仍然是空数组。因此,如果需要等待异步操作完成后再执行某些操作,建议使用 Promise 或 async/await 等方式来处理异步调用。
function getMchArr(){ const options = { hostname: 'localhost.manage.donotcall.com.cn', port: 80, path: '/ZhouBaoApi/GetConditionMch?start_time=20230522&end_time=20230528&avg_num=3000000', method: 'GET' }; var mch_arr = []; const req = http.request(options, (res) => { res.on('data', (chunk) => { var result = chunk.toString(); var result = JSON.parse(result); for (let i = 0; i < result.data.length; i++) { const item = result.data[i]; mch_arr.push(item.mch_id) console.log(1111) } }); }); req.on('error', (error) => { console.error(error); }); req.end(); console.log(222) return mch_arr; }1111还没打印出来就打印了222,怎么让他顺序执行
要让这段代码按照顺序执行,可以将返回 `mch_arr` 的操作放到请求响应的回调函数中,这样就可以等到请求返回后再返回 `mch_arr`。例如,可以将代码改为:
```
function getMchArr(callback) {
const options = {
hostname: 'localhost.manage.donotcall.com.cn',
port: 80,
path: '/ZhouBaoApi/GetConditionMch?start_time=20230522&end_time=20230528&avg_num=3000000',
method: 'GET'
};
var mch_arr = [];
const req = http.request(options, (res) => {
res.on('data', (chunk) => {
var result = chunk.toString();
var result = JSON.parse(result);
for (let i = 0; i < result.data.length; i++) {
const item = result.data[i];
mch_arr.push(item.mch_id)
console.log(1111)
}
callback(mch_arr); // 在请求回调函数中返回 mch_arr
});
});
req.on('error', (error) => {
console.error(error);
});
req.end();
}
getMchArr((mch_arr) => { // 调用 getMchArr 并在回调函数中处理返回的 mch_arr
console.log(mch_arr);
});
```
这样,当调用 `getMchArr` 时,它会发起一个 HTTP GET 请求,请求返回后会执行回调函数,并将 `mch_arr` 作为参数传入。在回调函数中,可以对 `mch_arr` 进行处理,例如打印出来。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)