How can I make an HTTP request within a fiber without a context switch?

540 views Asked by At

While running the following (simplified) code in meteor, I encountered an unexpected behaviour:

import { HTTP } from 'meteor/http';
Fiber = Npm.require('fibers');

let x = {};

Fiber(function() {
    x.a = 1;

    let result = HTTP.get('https://jsonplaceholder.typicode.com/posts/1');

    x.a = 2;
    console.log('fiber finished!');
}).run();

console.log(x);

Output:

{ a: 1 }
fiber finished!

HTTP.get is (supposed to be) synchronous according to it's documentation. I tried using another library instead of meteor/http but got the same result.

It seems like the execution within the fiber stops when it reaches the HTTP request and continues outside the fiber. Only after it finishes outside the fiber, it returns to finish up what it started within the fiber.

What's going on? How can I make an HTTP request within a fiber without this context switch?

0

There are 0 answers