获取承诺永远不会被执行

获取承诺永远不会被执行

本文介绍了获取承诺永远不会被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nativescript开发适用于android的应用程序.我有类似的东西

I am using the nativescript to develop an app for android.I have something like

var fetchModule = require("fetch");
fetchModule.fetch("http://202.120.227.11/")
    .then(function(resp){
        console.log(JSON.stringify(resp));
        return resp;
    })
    .catch(function(err){
        console.log(JSON.stringify(err));
        return err;
    });

,但then块永远不会执行.有时catch块会被执行并出现网络错误.但是无论哪种情况,都根据我的tcpdump记录发送了请求,并顺利收到了响应.

but the then block never gets executed.And sometimes the catch block gets executed and give a network error.But in either case, the request is sent and the response is smoothly received according to my tcpdump records.

因此,似乎本机脚本出于某种原因过滤了响应.

So, it seems the nativescript has filtered the response for some reason.

有人经历过吗?

推荐答案

请注意,您的resp响应对象,如果您想读取其内容,则需要使用其功能之一: arrayBuffer blob formData json 文本.

Note that your resp is a Response object, if you want to read its contents you need to use one of its functions: arrayBuffer, blob, formData, json, or text.

这些函数读取对完成的响应,并返回一个使用读取值进行解析的promise.

These functions read the response to completion and return a promise that resolves with the read value.

例如,

fetch("http://202.120.227.11/")
.then(function(resp){
  return resp.json();
})
.then(function(val) {
  console.log(JSON.stringify(val));
  return val;
});

这篇关于获取承诺永远不会被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 12:14