leancloud-js 与 parse-js 有点像啊
想去看看,函数如何统一处理 callback 和 promise 的办法,然后去找找了 leancloud 和 parse 的代码看看,乖乖,这两个真像。
- leancloud-js-sdk query.js
get: function(objectId, options) {
if(!objectId) {
var errorObject = new AV.Error(AV.Error.OBJECT_NOT_FOUND,
"Object not found.");
throw errorObject;
}
var self = this;
self.equalTo('objectId', objectId);
return self.first().then(function(response) {
if (!_.isEmpty(response)) {
return response;
}
var errorObject = new AV.Error(AV.Error.OBJECT_NOT_FOUND,
"Object not found.");
return AV.Promise.error(errorObject);
})._thenRunCallbacks(options, null);
},
- parse-js-sdk ParseQuery.js
get(objectId: string, options?: FullOptions): ParsePromise {
this.equalTo('objectId', objectId);
var firstOptions = {};
if (options && options.hasOwnProperty('useMasterKey')) {
firstOptions.useMasterKey = options.useMasterKey;
}
if (options && options.hasOwnProperty('sessionToken')) {
firstOptions.sessionToken = options.sessionToken;
}
return this.first(firstOptions).then((response) => {
if (response) {
return response;
}
var errorObject = new ParseError(
ParseError.OBJECT_NOT_FOUND,
'Object not found.'
);
return ParsePromise.error(errorObject);
})._thenRunCallbacks(options, null);
}
最后一句都是:_thenRunCallbacks(options, null);
心有灵犀一点通。
求大神科普 Parse中的代码:get(objectId: string, options?: FullOptions): ParsePromise
这句中,还带有类型?