Javascriptservices: What is the purpose of domain-task?

Created on 13 Aug 2017  路  2Comments  路  Source: aspnet/JavaScriptServices

In React-Redux-Spa template:

import { fetch, addTask } from 'domain-task';
...
            let fetchTask = fetch(`/api/SampleData/WeatherForecasts?startDateIndex=${ startDateIndex }`)
                .then(response => response.json() as Promise<WeatherForecast[]>)
                .then(data => {
                    dispatch({ type: 'RECEIVE_WEATHER_FORECASTS', startDateIndex: startDateIndex, forecasts: data });
                });

            addTask(fetchTask); // Ensure server-side prerendering waits for this to complete

Can you explain what is the purpose of fetch and addTask function, can I just use axios for fetch?

Most helpful comment

As you can read in last line addTask

Ensure server-side prerendering waits for this to complete

And regarding fetch method it adds some logic to make requests on server-side. When you run your code on server-side you cannot use relative URL because request it's made from node that doesn't know context. So fetch converts URL to absolute to make correct request.

If you remove server-side rendering there is no problem with using axios. Otherwise you have to write some similar logic like it is in fetch function.

All 2 comments

As you can read in last line addTask

Ensure server-side prerendering waits for this to complete

And regarding fetch method it adds some logic to make requests on server-side. When you run your code on server-side you cannot use relative URL because request it's made from node that doesn't know context. So fetch converts URL to absolute to make correct request.

If you remove server-side rendering there is no problem with using axios. Otherwise you have to write some similar logic like it is in fetch function.

Thanks for answering, @rosieks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

justinyoo picture justinyoo  路  3Comments

dantheman999301 picture dantheman999301  路  4Comments

Sampath-Lokuge picture Sampath-Lokuge  路  4Comments

dougabugg picture dougabugg  路  3Comments

benaadams picture benaadams  路  3Comments