당신은 jQuery와 Ember 's (RSVP) 약속 사이의 (어느 정도의) 비 호환성의 희생자입니다.
다음은 제약 조건은 다음과 같습니다
- 가 다른 jQuery를 약속을 흡수하는 jQuery를 약속.
- Ember.RSVP 약속 다른 Ember.RSVP 약속과 동화합니다.
- Ember.RSVP 약속은 약속/A + 호환이고 은 동의합니다. jQuery 약속.
- jQuery 약속 은에 동화되지 않습니다. Ember.RSVP 약속. jQuery 약속 체인은 반환 된 A + 약속을 데이터로 간주합니다.
는 여기에 몇 가지 주석으로, 문제의 코드입니다 :
return $.getJSON('config.json', (data) => {
//some code here...
return data; // a return from a direct callback is meaningless (though it doesn't matter here as `data` is not used later downstream).
}) // at this point, you have a jQuery promise.
.then(() => { // this is the jQuery promise's .then().
return this.get('store').findRecord('staffMember', 13); // this is an Ember.RSVP promise, which will be seen by the jQuery chain as data, not assimilated.
})
.then(record => {
console.log(record); // oh dear, `record` is actually a jQuery promise.
});
는 따라서 질문에 설명 된 증상은 - 약속이 콘솔에 기록됩니다.
해결 방법은 jQuery 약속이 다른 방법이 아닌 Ember.RSVP 체인으로 반환되도록하는 것입니다.
가 여기에 주로 문법에 차이를 코딩하는 방법의 커플 - 모두 작동합니다 : 약간 더 ecomomically,
return Ember.RSVP.Promise.resolve() // start the chain with a resolved Ember.RSVP promise.
.then(function() {
return $.getJSON('config.json'); // returned jQuery promise will be recognised as such and assimilated by the Ember.RSVP chain
})
.then(data => {
//some code here ...
return this.get('store').findRecord('staffMember', 13); // as expected, the returned RSVP promise will also be assimilated.
})
.then(record => {
console.log(record);
});
또는 :
return Ember.RSVP.Promise.resolve($.getJSON('config.json')) // assimilate jQuery promise into Ember.RSVP
.then(data => {
//some code here ...
return this.get('store').findRecord('staffMember', 13); // returned RSVP promise will also be assimilated
})
.then(record => {
console.log(record);
});
참고 : jQuery를 3.0, jQuery는 약속 약속/A + 준수를 위해 착수했습니다.
먼저 화살표 기능에서'this '를 사용할 때 조심해야합니다. 첫 번째'then' 단계의 함수는 순식간입니까? 아니면 비동기이며 약속 객체를 반환합니까? – Redu
처음부터 더 많은 코드를 추가했습니다. 다른 약속을 반환하는'getJson' 함수를 사용합니다. – Luisetelo
예.하지만'$ .getJSON'의 반환 값을 사용하지 않습니다. getRecord ('staffMember', 13);'시간에 해결할 약속을 반환합니다. 나는 이것이''정의되지 않는다 ''고 의심한다.'' – Redu