Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Last active Dec 24, 2020
Embed
What would you like to do?
Force Async function to be sync for react server component. From https://davidwells.io/snippets/forcing-async-functions-to-sync-in-node
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')
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
{
"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