$ httpリクエストを呼び出してJSONPを返すサービスがあります。 APIから返されるJSONPは{valid:true}か{valid:false}のいずれかです。私のコードは次のとおりです。コードを実行する前に約束を待つ
this.checkValid = function() {
return $http({
method: 'JSONP',
url: 'API' + '?callback=JSON_CALLBACK',
}).then(function (response) {
var temp = response.data.valid;
return temp; //returns true or false
}, function (response) {
console.log('something went wrong');
})
}
は私が応答に依存別のサービスがcheckValid()から戻ってきた:
var data = requestService.checkValid();
var valid;
//handling the promise
data.then(function (response) {
valid = response;
console.log('Inside the then block : ' + valid);
});
if (valid)
console.log('Valid!');
else
console.log('Not Valid!');
出力する(APIが有効戻った後はtrue):
'Not valid'
'Not valid'
'Not valid'
'Not valid'
'Inside the then block : true'
'Inside the then block : true'
'Inside the then block : true'
'Inside the then block : true'
then()
が完了するのを待つ方法を知りたい場合は、value
をtrue
またはfalse
に設定してから0に進んでください声明。
promise($ q)を使用して結果を返し、次に実行することができます。やってみました ? –