Angular 承诺回调未触发
Angular promise callback not firing
我有这个应用程序,它使用 $cordovaFileTransfer 将文件上传到服务器,然后将有关该文件的数据发送到同一服务器。文件传输正常。然后将数据发送到服务器,服务器响应。但是响应并没有返回到承诺回调。为什么?
$scope.sendPost = function(data) {
//first upload a file then send more data about the file
$cordovaFileTransfer.upload('http://example.com', 'myfile.txt', options)
.then(function(result) {
var promise = MyFactory.sendFileData(data);
});
promise.then(function(response) {
//we never make it to here
});
}
在 MyFactory 中:
service.sendFileData = function(data) {
return $http({
//bunch of parameters. This function works, data is sent to the server and a response received
}).then(function(response) {
//this is fired when the response is received from the server. All is good so far.
return.response.data
});
}
return service;
$cordovaFileTransfer.upload
returns 一个 promise 对象,你可以用它来建立 promise 链机制。
代码
$scope.sendPost = function(data) {
//get hold on `upload` function promise
var promise = $cordovaFileTransfer.upload('http://example.com', 'myfile.txt', options)
.then(function(result)) {
//return MyFactory.sendFileData promise here which will follow promise chaining
return MyFactory.sendFileData(data);
});
//promise.then will get call once `MyFactory.sendFileData` complete it
promise.then(function(response) {
//will get called once `sendFileData` complete its promise
});
}
这是因为您正在依赖另一个 promise 的 回调来启动 promise 并且..很可能在 promise 被初始化之前您正在附加一个回调 tot .. 所以在您附加回调时,promise 尚未初始化,即 promise
是 null
.. 所以在您的控制台中您会看到一个错误。 .
尝试做一些类似
的事情
var x = function(response) {
//we'll make it to here now...
}
$cordovaFileTransfer.upload('http://example.com', 'myfile.txt', options)
.then(function(result)) {
var promise = MyFactory.sendFileData(data);
promise.then(x);
});
您应该遵循@PankajParkar 的解决方案,尽管这是更好的方法...
$scope.sendPost = function(data) {
//first upload a file then send more data about the file
$cordovaFileTransfer.upload('http://example.com', 'myfile.txt', options)
.then(function(result)) {
return MyFactory.sendFileData(result.data);
})
.then(function(response) {
});
我有这个应用程序,它使用 $cordovaFileTransfer 将文件上传到服务器,然后将有关该文件的数据发送到同一服务器。文件传输正常。然后将数据发送到服务器,服务器响应。但是响应并没有返回到承诺回调。为什么?
$scope.sendPost = function(data) {
//first upload a file then send more data about the file
$cordovaFileTransfer.upload('http://example.com', 'myfile.txt', options)
.then(function(result) {
var promise = MyFactory.sendFileData(data);
});
promise.then(function(response) {
//we never make it to here
});
}
在 MyFactory 中:
service.sendFileData = function(data) {
return $http({
//bunch of parameters. This function works, data is sent to the server and a response received
}).then(function(response) {
//this is fired when the response is received from the server. All is good so far.
return.response.data
});
}
return service;
$cordovaFileTransfer.upload
returns 一个 promise 对象,你可以用它来建立 promise 链机制。
代码
$scope.sendPost = function(data) {
//get hold on `upload` function promise
var promise = $cordovaFileTransfer.upload('http://example.com', 'myfile.txt', options)
.then(function(result)) {
//return MyFactory.sendFileData promise here which will follow promise chaining
return MyFactory.sendFileData(data);
});
//promise.then will get call once `MyFactory.sendFileData` complete it
promise.then(function(response) {
//will get called once `sendFileData` complete its promise
});
}
这是因为您正在依赖另一个 promise 的 回调来启动 promise 并且..很可能在 promise 被初始化之前您正在附加一个回调 tot .. 所以在您附加回调时,promise 尚未初始化,即 promise
是 null
.. 所以在您的控制台中您会看到一个错误。 .
尝试做一些类似
的事情var x = function(response) {
//we'll make it to here now...
}
$cordovaFileTransfer.upload('http://example.com', 'myfile.txt', options)
.then(function(result)) {
var promise = MyFactory.sendFileData(data);
promise.then(x);
});
您应该遵循@PankajParkar 的解决方案,尽管这是更好的方法...
$scope.sendPost = function(data) {
//first upload a file then send more data about the file
$cordovaFileTransfer.upload('http://example.com', 'myfile.txt', options)
.then(function(result)) {
return MyFactory.sendFileData(result.data);
})
.then(function(response) {
});