如何将 ember-concurrency 与回调函数一起使用?
How can I use ember-concurrency with a callback function?
我想使用 ember-concurrency 来处理地址集合的批量验证。地址验证由第三方 API 完成,它调用服务器端函数,然后在服务器完成其工作后 'calls back' 向客户端发送。
这里似乎是使用 ember-concurrency 插件的地方,但我不知道如何正确使用它。我认为挑战在于服务器端进程启动后 api 立即调用服务器 returns 。我如何让 ember-concurrency 知道对服务器的调用和回调函数之间的连接,以便 'task' 等待回调完成作为任务已完成的标志?我现在使用代码的方式是,'results'(可以理解)始终为 null。
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';
export default class BatchAddressInputComponent extends Component {
@service api;
addressList = "";
//addressList populated from a TextArea
@task *DoAddressValidation ()
{
let results = yield this.api.BatchQueryAddresses(this.addressList);
alert(results); //not surprisingly, 'results' is always null
}
}
import Service from '@ember/service';
export default class ApiService extends Service {
_api;
//API *should* be available as a global object, imported as a <script> on
// application start-up.
constructor() {
super(...arguments);
this._api = API;
}
BatchQueryAddresses(addressList) {
this._api.BatchQueryAddress(addressList, 2, this._queryAddressCallback, null, 2000);
}
_queryAddressCallback(result, error) {
if (error) {
alert("Error: " + result);
return;
}
var j = JSON.stringify(result, null, ' ');
return(j);
}
}
您需要return一个Promise
。 new Promise(...)
构造函数正是为此而设计的:
BatchQueryAddresses(addressList) {
return new Promise((resolve, reject) => {
this._api.BatchQueryAddress(
addressList,
2,
(result, error) => {
if (error) {
reject("Error: " + result);
} else {
const j = JSON.stringify(result, null, ' ');
resolve(j);
}
},
null,
2000
);
});
}
我想使用 ember-concurrency 来处理地址集合的批量验证。地址验证由第三方 API 完成,它调用服务器端函数,然后在服务器完成其工作后 'calls back' 向客户端发送。
这里似乎是使用 ember-concurrency 插件的地方,但我不知道如何正确使用它。我认为挑战在于服务器端进程启动后 api 立即调用服务器 returns 。我如何让 ember-concurrency 知道对服务器的调用和回调函数之间的连接,以便 'task' 等待回调完成作为任务已完成的标志?我现在使用代码的方式是,'results'(可以理解)始终为 null。
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';
export default class BatchAddressInputComponent extends Component {
@service api;
addressList = "";
//addressList populated from a TextArea
@task *DoAddressValidation ()
{
let results = yield this.api.BatchQueryAddresses(this.addressList);
alert(results); //not surprisingly, 'results' is always null
}
}
import Service from '@ember/service';
export default class ApiService extends Service {
_api;
//API *should* be available as a global object, imported as a <script> on
// application start-up.
constructor() {
super(...arguments);
this._api = API;
}
BatchQueryAddresses(addressList) {
this._api.BatchQueryAddress(addressList, 2, this._queryAddressCallback, null, 2000);
}
_queryAddressCallback(result, error) {
if (error) {
alert("Error: " + result);
return;
}
var j = JSON.stringify(result, null, ' ');
return(j);
}
}
您需要return一个Promise
。 new Promise(...)
构造函数正是为此而设计的:
BatchQueryAddresses(addressList) {
return new Promise((resolve, reject) => {
this._api.BatchQueryAddress(
addressList,
2,
(result, error) => {
if (error) {
reject("Error: " + result);
} else {
const j = JSON.stringify(result, null, ' ');
resolve(j);
}
},
null,
2000
);
});
}