【发布时间】:2015-12-23 21:22:17
【问题描述】:
鉴于下面的代码示例,任何想法为什么从getBatchedContent 返回的值是null?
import rp from 'request-promise';
import _ from 'lodash';
function getBatchedContent(size, page) {
return Promise.resolve(fetchBatchedContent(size, page)); // Getting `null` here.
}
function fetchBatchedContent(size, page) {
var options = {
uri: `http://www.example.com/posts?type=vogue-fashion-shows&filter[posts_per_page]=${size}&page=${page}`
};
return rp(options)
.then(function (res) {
res = JSON.parse(res);
// res is an array of content objects.
return _.map(res, function (obj) { // logged value of `_.map()` is correct.
return transformContent(obj);
});
});
}
function transformContent(obj) {
return {
id: obj.ID,
title: obj.title,
// etc.
};
}
【问题讨论】:
-
仅供参考,
getBatchedContent()不会为您提供任何其他功能。您可以直接调用fetchBatchedContent()并获得相同的结果。 -
如果您正在谈论已解决的承诺值是
null,那么我会查看您的rp(...).then()处理,看看它是否真的在做您期望的事情并实际返回所需的值.
标签: javascript request return promise lodash