我需要在 AngularJS 1.0.7 中使用参数运行函数“searchBoats(boatType)”。此参数是另一个函数 parseBoatType 的结果,该函数正在运行一个使用 $resource 调用 API 的服务。当资源中返回带有 Promise 的 BoatType 时,如何运行 searchBoats?
这是我尝试过的:
var parseURL = function() {
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function success (result) {
console.log(result);
searchBoats(result);
});
deferred.resolve(
parseBoatType()
);
};
parseURL();
var parseBoatType = function() {
// Do some stuff
// Calculate boatType calling a service that uses resource to call
// an API
// I can convert this callback into a promise but still facing same
// issue
BoatType.getBoatTypeByName({name: boatTypeParsed}, function success(result) {
return result;
});
// The service method is called and the code is still running until
// the end of the function without waiting for the service result.
// Then the promise.then code in the parseURL is executed and
// searchBoats is run with boatType undefined.
};
// The service with the $resource call to the API
.factory('BoatType',
function($resource, SERVER_URL){
var boatTypes =
$resource('http://' + SERVER_URL +'/:action', {action:'boat_types'}, {
query: {method:'GET', isArray: true},
getBoatTypeByName: {method:'GET', params:{action: 'getBoatTypeByName'}, isArray: false}
});
return boatTypes;
}
)
最佳答案
您可以返回资源$promise
来自BoatType
资源在 parseBoatTime
函数并使用 Promise 来解决 parseUrl
推迟。
首先返回 parseBoatTime
的 promise 功能:
return BoatType.getBoatTypeByName({
name: boatTypeParsed
}, function success(result) {
return result;
}).$promise;
然后解析parseUrl
deferred
BoatType
的 promise 资源:
parseBoatType().then(deferred.resolve);
以下是从您的问题中获取的完整代码以及我提到的更正。
var parseURL = function() {
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function success(result) {
console.log(result);
searchBoats(result);
});
parseBoatType().then(deferred.resolve);
};
parseURL();
var parseBoatType = function() {
// Do some stuff
// Calculate boatType calling a service that uses resource to call
// an API
// I can convert this callback into a promise but still facing same
// issue
// Code for ngResource@^1.2.0
/*return BoatType.getBoatTypeByName({
name: boatTypeParsed
}, function success(result) {
return result;
}).$promise;*/
// Code for ngResource lower than 1.2.0
var deferred = $q.defer(), promise = deferred.promise;
BoatType.getBoatTypeByName({
name: boatTypeParsed
}, deferred.resolve, deferred.reject);
return promise;
// The service method is called and the code is still running until
// the end of the function without waiting for the service result.
// Then the promise.then code in the parseURL is executed and
// searchBoats is run with boatType undefined.
};
// The service with the $resource call to the API
app.factory('BoatType',
function($resource, SERVER_URL) {
var boatTypes =
$resource('http://' + SERVER_URL + '/:action', {
action: 'boat_types'
}, {
query: {
method: 'GET',
isArray: true
},
getBoatTypeByName: {
method: 'GET',
params: {
action: 'getBoatTypeByName'
},
isArray: false
}
});
return boatTypes;
}
)
关于javascript - AngularJS 1.0.7 中使用 $resources 嵌套 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46329186/