我开发了一个类来将文件上传到 imageshack。为此,您必须先进行授权以获得 auth_id。
因为我在 nodejs 中使用它,所以我不想在每次上传图像时都创建 auth_id。为此,我需要这样的东西:
function test(){
this.auth = "";
}
// Callback will execute when file-upload is finish
test.prototype.upload = function(file, callback) {
if(this.auth == "") {
/*** HERE I NEED HELP: ***/
this.make_auth(this.upload(file,callback));
}else{
// Do upload and then
callback();
}
}
test.prototype.make_auth = function(callback) {
// Make auth...
this.auth = "01020312032";
callback();
}
我想做的: - 检查是否设置了 this.auth - 如果 this.auth == ""完成授权并在完成原始参数后再次调用上传功能
我该怎么做?
最佳答案
改变:
this.make_auth(this.upload(_paramters));
类似于:
var self = this;
this.make_auth(function() { self.upload(_paramters); });
如果身份验证失败,您可能应该将可能的 err
传递给 make_auth
中的回调。
关于Javascript:在方法内部调用方法本身(使用原始参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23874254/