Fetch Promise Never Runs

I am using nativescript to develop an android application. 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; }); 

but then block will never be executed. And sometimes the catch starts and throws a network error. But in any case, the request is sent, and the response is smoothly received according to my tcpdump entries.

So it seems that nativescript has filtered the answer for some reason.

Has anyone experienced this?

+3
source share
2 answers

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

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

For instance,

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

I know this is very late, but I just wanted to post a message saying that someone is facing this problem. I ran into the same issue with NativeScript, and my solution was to protect my site / endpoints. The application security policy for transport requires a secure connection. Therefore http will not work. You must connect via https.

0
source

All Articles