gpt4 book ai didi

javascript - 发送响应后异步函数无法正确执行

转载 作者:行者123 更新时间:2023-12-03 05:20:18 25 4
gpt4 key购买 nike

我编写了一个名为deploy.js的文件,它通过其API将文件夹中的所有文件上传到github。它需要两个参数 - 用户的 accessToken 和要上传的文件夹。我通过 Passport-github 获取访问 token 。我有一个主 app.js 文件,deploy.js 可通过变量部署获得。 App.js 有一个调用部署函数的路由 /what 。问题是以下代码有效。

app.get('/what', function(req, res) {
//req.user.token contains the oauth token of the user
deploy(req.user.token, folder, function() {
res.send("completed");
});
});

但是如果我将 res.send() 放在回调之外,它就不会完全执行。

app.get('/what', function(req, res) {
deploy(req.user.token, folder, function() {
console.log("deploy completed");
});
res.send("Work being done");
})

这是我编写的deploy.js 文件。我使用 npm-github 库作为原生 github API 的 Node 包装器。

var distHelper = require('./dist.js');
var fs = require('fs');
var path = require('path');
var github = require('github');
var gh = new github();

module.exports = function(accessToken, folder, callback) {
//Authenticating using the oauth accessToken received above
gh.authenticate({ type:"oauth", token: accessToken});

var fullPath = distHelper.distPath + "/" + folder;
var eventName = folder.substr(folder.search("/") + 1);

//the below function returns a list of all files in a directory recursively
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var i = 0;

(function next() {
var file = list[i++];
if (!file) return done(null, results);
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
})();
});
};
//creating a repo

gh.repos.create({name: 'b', auto_init: 1}, function(err, response) {
if(err) {
console.log(err);
}

walk(fullPath, function(err, results) {
if(err) {
console.log(err);
}

//Base64 encoding of a file
function encode(file) {
var bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString('base64');
}

//Function which uploads file to the github one by one
function doOne() {
var elem = results.shift();
var fileName = elem.substr(elem.search(eventName)+eventName.length + 1);
console.log(fileName);
gh.repos.createFile({owner: "princu7", repo: "b", path: fileName, message: "please work", content:encode(elem)}, function(err, res) {
if(err) {
console.log(err);
}
else {
console.log("got response");
}
if(results.length > 0) {
doOne();
}
else {
// when all files are finished uploading, then the below function is called
callback();
}
});
}
doOne();
});
});
};

我用头撞墙寻找原因,但没能找到任何原因。这可能与关闭有关,但我不太确定。如果有人愿意提供帮助,我将非常感激。谢谢。

最佳答案

您应该使用websocket,如下所示:

  1. 在您的服务器中。 app.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){

socket.on('doplyment', (msg) => {
socket.emit('deploying', 'I will notify you after deployment');
setTimeout(function(){
socket.emit('doployed', 'your request done.')
}, 3000)
});
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

  1. 您的客户端。client.js

<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script>
var socket = io();
socket.on('connect', function(){

socket.emit('doployment', 'I am wating');
socket.on('deploying', function(mas){
console.log(mas);
});
socket.on('deployed', function (msg) {
console.log(msg);
});
})
</script>

关于javascript - 发送响应后异步函数无法正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com