嗨,我是这个迷人的“程序员”世界的新手。我开始构建一些简单的页面,这迫使我学习一些JS,我试图驯服节点,但它......对我来说很复杂。有人可以解释如何将代码的一部分传递到这个asyns世界的另一部分吗?我尝试过回调,我试过没有用,但也许我只是搞乱一些因为它对我来说是新的东西?
原始代码:
function ok(ifOk){
console.log(ifOk);
}
function test(doSomeTests){
console.log(doSomeTests);
}
let req1 = request1('http://www.facebook.com', function(err, resp, content){
if (resp){
if(content.indexOf('someUser') !== -1){
var ifOk = 'ok';
ok(ifOk);
var doSomeTests = 'tests';
}
}
else{
console.log(err);
}
});
let req2 = request2('http://www.google.com', function(err, resp, content){
if (resp){
// how to move do_some_tests here !?
// I would like to start those 2 request immediately, so req1 is downloading data, req2 is downloading data,
// and when req1 is finished and req2 is in the place it required the data to move forward - the data (doSomeTests) is passed in - is awailable to use
test(do_some_test);
}
else{
console.log(err);
}
});
带回调的代码:
// function after() {
// setTimeout(function() {
// console.log('after function - 3 sec setTimeout');
// }, 3000);
// }
function test(doSomeTests) {
console.log('doSomeTests is running: ' + doSomeTests);
}
function req1(_cb) {
request('https://www.facebook.com', function (err, resp, content) {
if (resp) {
console.log('req1 in process...');
if (resp.statusCode === 200) {
var doSomeTests = 'tests';
_cb(doSomeTests);
}
}
if (content) {
console.log('req1 is done!');
}
else {
reject(err);
}
});
}
function req2() {
request('https://www.google.com', function (err, resp, content) {
if (resp) {
console.log('req2 in process...');
// how to move do_some_tests here !?
// I would like to start those 2 requests immediately, so req1 is downloading data and also req2 is downloading data,
// and when req1 is finished and req2 is in the place it required the data to move forward - the data (doSomeTests) is passed in - is awailable to use
req1(test);
// after();
}
if (content) {
console.log('req2 is done!');
}
else {
console.log(err);
}
});
}
req2();
带请求的代码:
// function ok(ifOk) {
// setTimeout(function() {
// console.log(ifOk);
// }, 2000);
// }
// function after(){
// setTimeout(function(){
// console.log('after function - 3 sec setTimeout');
// }, 3000);
// }
function test(doSomeTests) {
console.log('doSomeTests is running: ' + doSomeTests);
}
function req1() {
return new Promise((resolve, reject) => {
request('https://www.facebook.com', function (err, resp, content) {
if (resp) {
console.log('req1 in process...');
if (resp.statusCode === 200) {
// var ifOk = 'ok';
// ok(ifOk);
var doSomeTests = 'tests';
resolve(doSomeTests);
}
}
if (content) {
console.log('req1 is done!');
}
else {
reject(err);
}
})
})
}
let req2 = req1().then(do_some_test => {
request('https://www.google.com', function (err, resp, content) {
if (resp) {
console.log('req2 in process...');
// I would like to start those 2 requests immediately, so req1 is downloading data, req2 is downloading data,
// and when req1 is finished and req2 is in the place it required the data to move forward - the data (doSomeTests) is passed in - is awailable to use
test(do_some_test);
// after();
}
if (content) {
console.log('req2 is done!');
}
else {
console.log(err);
}
});
});
无论如何请求都不是异步下载内容。如何修改上面的代码以获得评论中的结果?