There is no need for a variable, if it bothers you, you can return tuples (array in ES).
, .
const getData = data => {
return fetch('/api_endpoint',{
method: 'GET',
headers: {
'Content-type': 'application/json'
}
})
.then(response =>
Promise.all([
response.status,
response.json()
])
)
.then(
([status,body]) =>
({
body,
status
})
)
}
, :
const getData = data => {
return fetch('/api_endpoint',{
method: 'GET',
headers: {
'Content-type': 'application/json'
}
})
.then(response =>
response.json()
.then(
body=>({
body,
status:response.status
})
)
)
}
, await , . , , , , .
, json , json - , body/status, .
:
typeof JSON.parse(JSON.stringify({startDate:new Date()})).startDate
, , :
, url - a b .. .. :
a -> b -> c -> d ; [b,d]-> e
, a -> e, 4 :
a -> bb -> cc -> d[b,d] -> e
1 2 2 3 1.then(2).then(3) , 2 , 4.
, - a -> e, c -> d ( ) , [b,d] -> e .
( , , ). (a, b, c, d, e) , a b, b c... a-c a b b c, , [b,d] e
objectAndResponseToObjectAndStatusObject, ( 1- ) ( 3- ) thread, createThread.
const promiseLike = val =>
(val&&typeof val.then === "function");
const REPLACE = {};
const SAVE = {}
const createThread = (saved=[]) => (fn,action) => arg =>{
const processResult = result =>{
const addAndReturn = result => {
(action===SAVE)?saved = saved.concat([result]):false;
(action===REPLACE)?saved = [result]:false;
return result;
};
return (promiseLike(result))
? result.then(addAndReturn)
: addAndReturn(result)
}
return (promiseLike(arg))
? arg.then(
result=>
fn(saved.concat([result]))
)
.then(processResult)
: processResult(fn(saved.concat([arg])))
};
const jsonWithActualDates = keyIsDate => object => {
const recur = object =>
Object.assign(
{},
object,
Object.keys(object).reduce(
(o,key)=>{
(object[key]&&(typeof object[key] === "object"))
? o[key] = recur(object[key])
: (keyIsDate(key))
? o[key] = new Date(object[key])
: o[key] = object[key];
return o;
},
{}
)
);
return recur(object);
}
const testJSON = JSON.stringify({
startDate:new Date(),
other:"some other value",
range:{
min:new Date(Date.now()-100000),
max:new Date(Date.now()+100000),
other:22
}
});
const urlToResponse = url =>
Promise.resolve({
status:200,
json:()=>JSON.parse(testJSON)
});
const responseToObject = response => response.json();
const objectWithDates = object =>
jsonWithActualDates
(x=>x.toLowerCase().indexOf("date")!==-1||x==="min"||x==="max")
(object);
const objectAndResponseToObjectAndStatusObject = ([response,object]) =>
({
body:object,
status:response.status
});
const getData = (url) => {
const thread = createThread();
return Promise.resolve(url)
.then( thread(urlToResponse,SAVE) )
.then( responseToObject )
.then( objectWithDates )
.then( thread(objectAndResponseToObjectAndStatusObject) )
};
getData("some url")
.then(
results=>console.log(results)
);
Hide resultgetData :
const getData = async (url) => {
const response = await urlToResponse(url);
const data = await responseToObject(response);
const dataWithDates = objectWithDates(data);
return objectAndResponseToObjectAndStatusObject([response,dataWithDates]);
};
: getData ? , getData , , url , ... GetData .
getData, , , :
const getData = (url) =>
urlToResponse(url).then(
response=>
responseToObject(response)
.then(objectWithDates)
.then(o=>objectAndResponseToObjectAndStatusObject([response,o]))
);
, , getDate.
( compose ) . , , , .
, , . ( ), 1 , , .
:
const getDataFunctions = [
[pipe([setPage,setFiler]),SET_PARAMS],
[makeRequest,MAKE_REQUEST],
[setResult,SET_RESULTS],
];
. :
const initialLoad = (action,state) =>
pipe(getDataFunctions.map(([fn])=>fn))([action,state]);
:
const pageChanged = action =>
pipe(getDataFunctions.map(
([fn,type])=>{
if(type===SET_PARAMS){
return setPage
}
return fn;
}
))([action,state]);
const filterChanged = action =>
pipe(getDataFunctions.map(
([fn,type])=>{
if(type===SET_PARAMS){
return setFiler
}
return fn;
}
))([action,state]);
, , , . InitialLoad , ( ), pageChanged , , , filterChanges , .
, , , ?
const getDataFunctions = [
[pipe([setPage,setFiler]),SET_PARAMS],
[fromCache(makeRequest),CACHE_OR_REQUEST],
[setResult,SET_RESULTS],
];
getData pipe thread ( , ).
const getData = url => {
const thread = createThread();
return pipe([
thread(urlToResponse,SAVE),
responseToObject,
objectWithDates,
thread(objectAndResponseToObjectAndStatusObject)
])(url);
};
JavaScript, , T->T, , , threaded go a b c.
- F # ReasonML, , , .