Page 1 of 1

[Unrelated] Weird problem with fetch()/promise chaining

Posted: Sat Aug 26, 2017 2:34 am
by ZeroUnderscoreOu
I'm not sure if this is a problem from the last update, but I do think this is related to NoScript.
I noticed it while writing a small userscript for Imgur, so the code I quote assumes an image album open (imgur.com/a/*, imgur.com/gallery/*) and is run in Greasemonkey.

Code: Select all

var Hash = document.location.pathname.match(/(gallery|a)\/(\w+)/)[2];
var Link = `${document.location.origin}/ajaxalbums/getimages/$Hash/hit.json`;
fetch(Link).then((Data)=>(Data.text())).then((Data)=>{
    console.log("Log",Data.length);
});
It runs as I expect it to while base domain (Imgur itself) is allowed. When it's not, script silently fails without any messages in page/browser consoles. Now, the interesting part is that if I change the code a bit, it starts working:

Code: Select all

fetch(Link).then((Data)=>{
    Data.text().then((Data)=>{
        console.log("Log",Data.length);
    })
});
Second then() is put right after text() instead of being chained outside and parentheses are replaced with braces.

There might be some stupid mistake from me, but the inconsistency makes me think this is a thing with NoScript (including the partial blocking of code pasted into page console, but it might be a different topic).

Win 7 x64, FF 55.0.3 x64, NoScript 5.0.9, Greasemonkey 3.11.

Re: Weird problem with fetch()/promise chaining

Posted: Sat Aug 26, 2017 4:06 am
by barbaz
I don't recall the first way ever working for me.

Re: Weird problem with fetch()/promise chaining

Posted: Sat Aug 26, 2017 4:56 am
by ZeroUnderscoreOu
It does for me (with scripts allowed):
Image
And you reminded me about another strange thing I forgot to mention: both with code pasted into console and run as a userscript it shows the log message as if it came from global.js. Though it may be caused by some library used on Imgur.

Re: Weird problem with fetch()/promise chaining

Posted: Sat Aug 26, 2017 5:17 am
by barbaz
Checked again, it does work both ways with scripts enabled. But I don't get it working either way with scripts disabled. I'm using the Web Console for testing.

I tested in a clean profile without NoScript, and setting about:config > javascript.enabled to false produced the same results. So this is not a NoScript issue.

Re: Weird problem with fetch()/promise chaining

Posted: Sat Aug 26, 2017 5:38 am
by ZeroUnderscoreOu
barbaz wrote:So this is not a NoScript issue.
Yeah, looks like.
Checked myself the same way (didn't know about script preference) and FF's behavior seems really counterintuitive to me. But I guess it's a thing to complain to Mozilla instead.
Tnx.