Last active
December 24, 2020 03:05
-
-
Save DavidWells/c0cfa9e57243672101b9e2587037911a to your computer and use it in GitHub Desktop.
Force Async function to be sync for react server component. From https://davidwells.io/snippets/forcing-async-functions-to-sync-in-node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const forceSync = require('sync-rpc') | |
const syncFunction = forceSync(require.resolve('./async-thing')) | |
console.log('before') | |
const paramOne = 'foo' | |
const paramTwo = 'bar' | |
const syncReturnValue = syncFunction(paramOne, paramTwo) | |
console.log('syncReturn', syncReturnValue) | |
console.log('after') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk') | |
const dynamoDB = new AWS.DynamoDB({ | |
region: 'us-east-1' | |
}) | |
async function doThing() { | |
return (paramOne, paramTwo) => { | |
// Your async code here | |
return dynamoDB.describeTable({ TableName: 'blog-posts' }).promise().then((x) => { | |
return x | |
}) | |
} | |
} | |
module.exports = doThing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "force-syncer", | |
"version": "1.0.0", | |
"main": "index.js", | |
"dependencies": { | |
"aws-sdk": "^2.817.0", | |
"sync-rpc": "^1.3.6" | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment